【问题标题】:Call to undefined method dbConnection::query() - How to fix it?调用未定义的方法 dbConnection::query() - 如何解决?
【发布时间】:2013-03-14 08:45:45
【问题描述】:

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

这是完整的错误声明:

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

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

【问题讨论】:

  • 使用 mysqli_* 函数重写你的代码。 mysql_* 函数自 php 5.4 起已弃用。
  • 请包含类文件。

标签: php mysql database oop web


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

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

    进行测试
    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>
    

    【讨论】:

      猜你喜欢
      • 2019-10-20
      • 2017-05-28
      • 1970-01-01
      • 2016-05-29
      • 2018-12-21
      • 1970-01-01
      • 1970-01-01
      • 2014-04-23
      • 1970-01-01
      相关资源
      最近更新 更多