本人初学,正在尝试自己做一个论坛,很多地方不懂,这是我定义的数据库类:
<?PHP
class content{
    var $conn;
public $contid;
public $subject;
public $words;
public $username;
public $face;
public $email;
public $homepage;
public $createtime;
public $upperid;

function _construct(){
        $this->mysqli_connect("localhost","root","","book");
    mysqli_query($this->conn,"SET NAMES gbk");
}
    function _destruct(){
        mysqli_close($this->conn);
}
function getinfo($id){
    $sql="select * from content where contid='".$id;
$result=$this->conn->query($sql);
if($row=$result->fetch_row())
{
    $this->contid=$id;
$this->subject=$row[1];
$this->words=$row[2];
$this->username=$row[3];
$this->face=$row[4];
$this->email=$row[5];
$this->homepage=$row[6];
$this->createtime=$row[7];
$this->upperid=(int)$row[8];
}
}
function getrecordcount(){
    $sql="select count(*) from content";
$result=$this->conn->query($sql);
if($row=$result->fetch_row())
    return (int)$row[0];
else
    return 0;
}
function insert(){
    $sql="insert into content (subject,words,username,face,email,homepage,createtime,upperid) values ('".$this->subject."','".$this->words."','
".$this->username."','".$this->face."','".$this->email."','".$this->homepage."','".$this->createtime."','".$this->upperid."')";
$this->conn->query($sql);
}
function delete($id){
    $sql="delete from content where contid=".$id."or upperid".$id;
$this->conn->query($sql);
}
function load_content_byupperid($uid){
    $sql="select * from content where upperid=".$uid."order by createtime desc";
$result=$this->conn->query($sql);
return $result;
}
function load_content_bypage($pageno,$pagesize){
    $sql="select * from content order by createtime desc limit".($pageno-1)*$pagesize.",".$pagesize;
$result=$this->conn->query($sql);
return $result;
}
}
?>
然后在另一个网页使用,include调入上面这个网页时,出现了这样一个错误:Fatal error: Call to a member function query() on a non-object ,
我应该已经实例化类了呀,为什么还是不能调用呢?

解决方案 »

  1.   

    不说别的,从一开始就有问题
    function _construct(){
      $this->mysqli_connect("localhost","root","","book");
    mysqli_query($this->conn,"SET NAMES gbk");
    }mysqli_connect有定义么,
      

  2.   

    mysqli_connect()不是连接数据库的函数吗,直接能用的呀?
      

  3.   

    哦,打错了,改成这个$this->conn=mysqli_connect("localhost","root","","book");不过还是同样的错误,求解决?
      

  4.   

    错误提示在这行上,红字体
    function getrecordcount(){
        $sql="select count(*) from content";
    $result=$this->conn->query($sql); if($row=$result->fetch_row())
        return (int)$row[0];
    else
        return 0;
    }
      

  5.   

    function __construct(){
      $this->conn = mysqli_connect("localhost","root","","book");
      mysqli_query($this->conn,"SET NAMES gbk");
    }
      

  6.   

    $this->conn    这个,从头到尾都买见你赋值过.........