时间:2021-07-01 10:21:17 帮助过:21人阅读
输出数据
// class_user.php
class UserInfo{
private $userName; //属性,用户名
private $passWord; //属性,
private $userGrade; //属性,用户级别
private $row;
private $userInfo; //存储数据库返回信息的数组变量.
public function __construct($sql){
require_once("db_config.php"); //包含配置信息.
$result= mysql_query($sql, $conn); // 执行查询语句
$this->userInfo = mysql_fetch_array($result); //返回查询结果到数组
$row=mysql_num_rows($result);
mysql_query("set names UTF8");
mysql_close($conn); //关闭数据库连接
$this->getInfo(); //调用传递信息的方法.
}
// 获取信息传递给属性的方法
private function getInfo(){
$this->userName = $this->userInfo["username"];
$this->passWord = $this->userInfo["password"];
$this->userGrade = $this->userInfo["userGrade"];
}
//返回每个属性的public 方法.
public function getRow(){
return $this->row;
}
public function getUserName(){
return $this->userName;
}
public function getPassword(){
return $this->passWord;
}
public function getUserGrade(){
return$this->userGrade;
}
}
$sql= "select * from userinfo where username='sa' ";
require_once("class_user.php");
$user= new UserInfo($sql); //创建一个user对象.
//书写sql
$row= $user->getRow();
$username= $user->getUserName(); //分别调用方法取得数据
$password= $user->getPassword();
$usergrade= $user->getUserGrade();
echo"your name is ".$username."
"; //