0

我刚来这地方。我正在开发的网站有问题。你能帮我解决一下吗。

这是完整的错误声明:

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

我以面向对象的方式使用 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);
    }
}
?>
4

2 回答 2

1

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

function __construct(){
include_once('dbConnection.php");
    $this->db = new dbConnection();
    $this->db->connect();
}
于 2013-03-14T08:55:54.277 回答
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>
于 2013-03-14T09:38:35.390 回答