调用未定义的方法dbConnection :: query() - 如何修复它?

时间:2013-03-14 08:45:45

标签: php mysql database oop web

我是新来的。我正在开发我的网站时遇到问题。你能帮我解决一下吗?

这是完整的错误陈述:

  

致命错误:调用未定义的方法dbConnection :: query()       第20行的C:\ xampp \ htdocs \ koa \ classes \ class.ManageUsers.php。

我以面向对象的方式使用PHP和MySQL。

以下是错误指向的class.ManageUsers.php中的代码。我将把整个函数放在这里:。

function LoginUsers($username,$password){
    $query = $this->db->query("SELECT * FROM users WHERE username = '$username' AND password = '$password'");
    $num_rows = $this->link->fetchRows();
    return $num_rows;
}

第20行是:

$query = $this->db->query("SELECT * FROM users WHERE username = '$username' AND password = '$password'");

这里的构造函数:

function __construct(){
    $this->db = new dbConnection();
    $this->db->connect();
}

dbConnection类是这样的:

class dbConnection{
    protected $db_conn, $_query, $_numRows, $_fetchAll;
    public $db_name = '******';
    public $db_user = '******';
    public $db_pass = '******';
    public $db_host = '******';

    function connect(){ 
        $this->db_conn = mysql_connect($db_host, $db_user, $db_pass);
        mysql_select_db($db_name, $this->db_conn);
        if(mysql_errno($this->db_conn){
            return mysql_error($this->db_conn);
        }
        else{
            return $this->db_conn;
        }
    }

    public function query($sql){
        $this->_query = mysql_query($sql, $this->db_conn);
        $this->_numRows = mysql_num_rows($this->_query);
        $this->_fetchAll = mysql_fetch_array($this->_query);
    }
}
?>

2 个答案:

答案 0 :(得分:1)

请尝试使用以下代码 给出dbConnection.php文件的正确路径,这只是示例代码。

function __construct(){
include_once('dbConnection.php");
    $this->db = new dbConnection();
    $this->db->connect();
}

答案 1 :(得分:0)

看起来您没有使用您期望的课程版本。你可以通过

进行测试
class ... {
  function LoginUsers($username,$password){
    ..
    foo($this->db, 'query');
    $query = $this->db->query("SELECT * FROM users ...")
    ..
  }
  ...
}

function foo($obj, $testFnExists=null) {
    $abort = false;
    $ro = new ReflectionObject($obj);
    printf("<pre>\nClass: %s\n", $ro->getName()); 
    printf("defined at %s@%d\n", $ro->getFileName(), $ro->getStartLine());
    printf("object has the following public methods:\n");
    foreach( $ro->getMethods(Reflectionmethod::IS_PUBLIC) as $m ) {
        printf("  %s\n", $m->getName());
    }
    if ( !is_null($testFnExists) ) {
        if ( !$ro->hasMethod($testFnExists) ) {
            $abort = true;
            $methodExists = 'no';
        }
        else {
            $methodExists = 'yes';
        }
        printf("method '%s' exists: %s\n", $testFnExists, $methodExists);
    }
    printf("</pre>\n");
    flush();
    if ( $abort ) {
        die;
    }
}

输出应该是

<pre>
Class: dbConnection
defined at test.php@4
object has the following public methods:
  foo1
  foo2
method 'query' exists: no
</pre>