<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8"/>
</head>
<body>
<?php
class MysqlConn {
private $conn;
private $my_db;
private $result;
public function __construct() {
require_once("db_config.php");
$this->conn = mysql_pconnect($db_server, $db_user, $db_pwd);
$this->my_db = mysql_select_db($db_name,$this->conn);//選擇數據庫
mysql_query("SET NAMES utf8");
}
public function query($sql){
$this->result = mysql_query($sql, $this->conn); // 執行查詢語句
return $this->result;
}
public function next(){
return $row = mysql_fetch_array($this->result);
}
public function close(){
mysql_free_result($this->result);
}
//使用轉義字符,保證系統安全.
public function escapeString($str){
return mysql_escape_string($str);
}
}
class UserInfo{
protected $userLogin; //屬性,用戶名
protected $userPwd ; //屬性,用戶密碼
protected $userSex ; //屬性,用戶年齡
protected $userClass ; //屬性,用戶級別
protected $userInfo; //存儲數據庫返回信息的數組變量.
protected $mysqlConn;
public function __construct($name){
$this->mysqlConn = new MysqlConn();
$sql = "SELECT * FROM `userinfo_t` WHERE `userLogin` LIKE '$name'"; //查詢的sql
$rs = $this->mysqlConn->query($sql);
$this->userInfo = $this->mysqlConn->next();
$this->getInfo(); //調用傳遞信息的方法.
}
// 獲取信息傳遞給屬性的方法
protected function getInfo(){
$this->userLogin = $this->userInfo["userLogin"];
$this->userPwd = $this->userInfo["userPwd"];
$this->userSex = $this->userInfo["userSex"];
$this->userClass = $this->userInfo["userClass"];
}
//返回每個屬性的public 方法.
public function getUserLogin(){
return $this->userLogin;
}
protected function getUserPwd(){
return $this->userPwd;
}
public function getUserSex(){
return $this->userSex;
}
public function getUserClass(){
return $this->userClass;
}
}
?>
</body>
</html>
來源 :http://www.nowamagic.net/php/php_CreateClassUser2.php