mysqli_num_rows()期望参数1为mysqli_result,boolean given&查询失败

时间:2012-01-05 22:07:47

标签: php mysql mysqli

我曾经在一个类中的每个函数中声明我的MySQLi连接,但是已经改为在主类中使用了一个数据库连接。

但是,以前工作的查询现在返回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);
    }

这里是班级&amp;正在运行查询的函数:

<?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...但查询不再有效时查询有效。

有人能看到问题吗?提前致谢

1 个答案:

答案 0 :(得分:0)

很简单,if中的初始query()永远不会为真,因为$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()以外的所有内容中除去对数据库连接的检查,它是实际使用数据库连接的唯一函数,应该是检查连接的唯一函数。

其次,不是简单地将$this->query_id设置为来自$this->DB->query()的结果,而是检查结果是否为假。如果结果返回false,则从$this->DB->error转储一些错误信息,以确定究竟什么不起作用。