Chinaunix

标题: php oo 学习 [打印本页]

作者: liuxingyuyuni    时间: 2007-01-15 14:35
标题: php oo 学习


config.php
?php
    $server = "localhost";
    $user     = "root";
    $pass     = "wowangle";
    $db     = "test";
?>

oo.php

?php
    class UserInfo
    {
        private     $name;
        private     $pass;
        private     $age;
        private     $tel;
        private     $userInfo;
        
        public function __construct($username)
        {
            require("config.php");
            $conn = new mysqli($server, $user, $pass, $db);
            $conn->query('set names gb2312');
            $res = $conn->query("SELECT * FROM test where name='$username'");
            $this->userInfo = $res->fetch_assoc();
            $conn->close();
            $this->getUserInfo();
        }
        public function getUserInfo()
        {
            $this->name         = $this->userInfo['name'];
            $this->pass        =    $this->userInfo['pass'];
            $this->age        =    $this->userInfo['age'];
            $this->tel        =    $this->userInfo['tel'];
        }
        public function getUserName()
        {
            return $this->name;
        }
        
        public function getUserPass()
        {
            return $this->pass;
        }
        
        public function getUserAge()
        {
            return $this->age;
        }
        public function getUserTel()
        {
            return $this->tel;
        }
    }
    $user = new UserInfo('小破孩');
    echo $user->getUserName();
    echo $user->getUserTel();
   
?>

*注意编码,否则SQL进入数据库时会出错...


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/10599/showart_232283.html




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2