2012-01-05 48 views
0

我曾经在一个类中的每个函数中声明了我的MySQLi连接,但已经更改为在主类中使用了一个数据库连接。mysqli_num_rows()期望参数1是mysqli_result,布尔给定&查询失败

但是,之前运行的查询现在返回FALSE,导致mysqli_num_rows失败。

这里是我的连接类:

<?php 
class main{ 
    // the configuration vars 
    public $config = array(); 
    // database variables 
    public $DB; // contains the connection 
    public $query_id; // contains the query ID 
    public $query_count; // how many queries have there been? 
    // connect to database 
    public function connectToDatabase(){ 
     // connect to database 
     $this->DB = new mysqli($this->config['host'],$this->config['user'],$this->config['pass'],$this->config['db']); 
     // were we able to connect? 
     if(!$this->DB){ 
      // this is an error 
      exit('could not connect to the database.'); 
     } 
    } 
    // perform a query 
    public function query($query){ 
     // check we're connected 
     if(!$this->DB){ 
      $this->connectToDatabase(); 
     } 
     // perform query 
     $this->query_id = $this->DB->query($query); 
     // did it succeed? 
     // increase query count 
     $this->query_count ++; 
     // return 
     return $this->query_id; 
    } 
    // get number of rows 
    public function num_rows($query_id){ 
     // check we're connected 
     if(!$this->DB){ 
      $this->connectToDatabase(); 
     } 
     echo mysqli_num_rows($query_id); 
     // get number of rows 
     return mysqli_num_rows($query_id); 
    } 
    // gets the results array 
    public function results_array($query_id = ''){ 
     // check we're connected 
     if(!$this->DB){ 
      $this->connectToDatabase(); 
     } 
     // check we've set a query ID 
     if(!$query_id){ 
      $query_id = $this->query_id; 
     } 
     // get the results 
     $results = mysqli_fetch_assoc($query_id); 
     // return 
     return $results; 
    } 
    // escape string 
    public function escape_string($s){ 
     // check we're connected 
     if(!$this->DB){ 
      $this->connectToDatabase(); 
     } 
     $s2 = mysqli_real_escape_string($s); 
     return mysqli_real_escape_string($s); 
    } 

这里是一个正在运行的查询类&功能:

<?php 
class forum extends main{ 
    public function viewTopic($tid){ 
     // Get topic details 
     //$tid = intval($this->escape_string($tid)); 
     $tid = intval($tid); 
     $tq = "SELECT * FROM `topics` WHERE id='$tid' LIMIT 1;"; 
     $tq = $this->query($tq); 
     $tr = $this->results_array($tq); 
     // Does this topic exist? 
     if($this->num_rows($tq) == 0){ 

查询工作时,我有$sql = new...在的顶线功能,但不再工作。

任何人都可以看到问题吗?在此先感谢

回答

0

非常简单的初始ifquery()永远不会是真实的,因为$this->DB是空的,因此不是错误的。


public function query($query){ 
    if(!$this->DB){ 
     // this never gets ran because $this->DB is not false...it is null 
     $this->connectToDatabase(); 
    } 

    // more code here 

} 

它需要被改为:

public function query($query) { 
    if (!isset($this->DB)) { 
     $this->connectToDatabase(); 
    } 

    // more code here 

} 

回答OP的评论:

首先,摆脱了检查,在一切连接到数据库但是query(),它是实际使用数据库连接的唯一函数,应该是唯一一个签入g连接。

二,而不是简单地设置$this->query_id无论结果来自$this->DB->query()检查,看看结果是否为假。如果结果返回错误转储从$this->DB->error一些错误信息找出什么是不工作。

+0

谢谢,我做了这个改变,但它仍然返回0作为num_rows,给出了相同的错误。 connectToDatabase函数已在index.php中调用,但我没有在我的文章中包含它。 – Peter 2012-01-05 22:24:23

+0

@Peter你在'$ this-> config'中是否有某种你没有在样本中显示的值? – cspray 2012-01-05 22:26:35

+0

$ config包含主机,用户,数据库,密码和一些非数据库相关的功能。它在index.php上设置。 (在类首次声明之后) – Peter 2012-01-05 22:28:14

相关问题