关于PHP面向对象的一个问题
发布网友
发布时间:2022-04-06 02:43
我来回答
共2个回答
热心网友
时间:2022-04-06 04:12
调试一下其实就可以很快得到答案的!
zend只类的 一步步调试
第一步应该先echo sql 执行看看
第二步echo 数据集合 看看有没有得到array
//以下测试成功
<?php
define('DBHOST','192.168.1.1');
define('DBUSER','asktang');
define('DBPW','asktang123');
define('DBNAME','wxt_115');
function connect($dbhost = DBHOST, $dbuser = DBUSER, $dbpw = DBPW, $dbname = DBNAME )
{
$conn = @mysql_connect($dbhost,$dbuser,$dbpw) or die("无法连接到数据库!");
mysql_query("SET character_set_connection='gbk', character_set_results='gbk', character_set_client=binary");
mysql_query("SET sql_mode=''");
mysql_select_db($dbname,$conn) or die("无法选择数据库!");
}
function close()
{
return mysql_close();
}
connect();
?>
<?
class UserInfo
{
private $userName; //属性,用户名
private $userInfo; //存储数据库返回信息的数组变量.
public function __construct($uid)
{
$sql="select count(*) from supe_spaceitems where uid=$uid";
$result = mysql_query($sql);
$this->userInfo = mysql_fetch_array($result); //返回查询结果到数组
close();
$this->getInfo(); //调用传递信息的方法.
}
// 获取信息传递给属性的方法
private function getInfo()
{
$this->userName = $this->userInfo[0]; //这边你用0看看
}
热心网友
时间:2022-04-06 05:30
private function getInfo(){
$this->userName = $this->userInfo["username"];
$this->userPSW = $this->userInfo["userpsw"];
$this->userAge = $this->userInfo["userage"];
$this->userGrade = $this->userInfo["usergrade"];
}
改成
private function getInfo(){
$this->userName = $this->userInfo["name"];
$this->userPSW = $this->userInfo["password"];
$this->userAge = $this->userInfo["age"];
$this->userGrade = $this->userInfo["grade"];
}