+ -
当前位置:首页 → 问答吧 → PHP调用自己定义的一个数据库类时出现错误?

PHP调用自己定义的一个数据库类时出现错误?

时间:2011-09-09

来源:互联网

本人初学,正在尝试自己做一个论坛,很多地方不懂,这是我定义的数据库类:
<?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 ,
我应该已经实例化类了呀,为什么还是不能调用呢?

作者: abc15067436552   发布时间: 2011-09-09

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

mysqli_connect有定义么,

作者: hikklong   发布时间: 2011-09-09

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

作者: abc15067436552   发布时间: 2011-09-09

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

作者: abc15067436552   发布时间: 2011-09-09

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

作者: abc15067436552   发布时间: 2011-09-09

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

作者: xuzuning   发布时间: 2011-09-09

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

作者: yunprince   发布时间: 2011-09-09

相关阅读 更多