0

我有一个示例代码:

class DBConnect {
    private $connect = null;
    private $query = null;
    public $result = 0;
    public $data = null;
    public $_result;

    function connect() {
        $this->connect = mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASS) or die("Can't connect database");
        mysql_query("set names 'utf8'");
        mysql_select_db(MYSQL_NAME, $this->connect);
    }

    function close() {
        if($this->connect) {
            mysql_close($this->connect);    
        }
    }

    function query($sql) {
        $this->query = mysql_query($sql);
        if(!$this->query) {
            echo "Error: " . mysql_error();
            exit;
        }
    }

    function get_data($category_id) {
        $sql = 'SELECT id, name FROM category WHERE id='.$category_id;
        query($sql); // This is error
        $row = mysql_fetch_object($query);
        $data = $row->category;
        return $data;
    }
}

当我运行调试时错误是:Call to undefined function query() in line...,如何修复它?

4

2 回答 2

2

您可以更改功能查询-> query1

function query1($sql) { 
        $this->query = mysql_query($sql); 
        if(!$this->query) { 
            echo "Error: " . mysql_error(); 
            exit; 
        } 
    } 

进而:

function get_data($category_id) { 
        $sql = 'SELECT id, name FROM category WHERE id='.$category_id; 
        $this->query1($sql); // Error has repair
        $row = mysql_fetch_object($query); 
        $data = $row->category; 
        return $data; 
    } 
于 2012-05-14T03:23:30.827 回答
-1

在这部分代码中

function get_data($category_id) {
        $sql = 'SELECT id, name FROM category WHERE id='.$category_id;
        $this->query($sql); // This is error -> this is suppose to
        $row = mysql_fetch_object($this->query); // this also
        $data = $row->category;
        return $data;
    }
于 2012-05-14T03:19:42.043 回答