【问题标题】:PHP Call to undefined functionPHP调用未定义的函数
【发布时间】:2015-11-18 07:10:54
【问题描述】:

我在一个名为 Db.php 的类中有一个名为 getUserIds 的方法。

public function getUserIds() {
    $connection = $this -> connect();
    return $this -> select("select distinct user_id from users");
}

当我在与Db.php 位于同一文件夹中的文件中调用函数时,我得到了

PHP 致命错误:调用未定义的函数 getUserIds()

产生错误的文件如下所示:

<?php
    require('Db.php');
    $db = new Db();
    $userIds = $db -> getUserIds();
    echo '<select>';
    echo '<option value="">Choose your User Id</option>
    while($userId = mysqli_fetch_assoc($userIds)) {
        echo "<option>$userId</option>";
}
echo '</select>';
?>

这里是Db.php的内容:

class Db {
    // The database connection
    protected static $connection;

    /**
     * Connect to the database
     * 
     * @return bool false on failure / mysqli MySQLi object instance on success
     */
    public function connect() {    
        // Try and connect to the database
        if(!isset(self::$connection)) {
            // Load configuration as an array. Use the actual location of your configuration file
            $config = parse_ini_file('./config.ini'); 
            self::$connection = new mysqli('localhost',$config['username'],$config['password'],$config['dbname']);
        }

        // If connection was not successful, handle the error
        if(self::$connection === false) {
            // Handle error - notify administrator, log to a file, show an error screen, etc.
            return false;
        }
        return self::$connection;
    }

    /**
     * Query the database
     *
     * @param $query The query string
     * @return mixed The result of the mysqli::query() function
     */
    public function query($query) {
        // Connect to the database
        $connection = $this -> connect();

        // Query the database
        $result = $connection -> query($query);

        return $result;
    }

    /**
     * Fetch rows from the database (SELECT query)
     *
     * @param $query The query string
     * @return bool False on failure / array Database rows on success
     */
    public function select($query) {
        $rows = array();
        $result = $this -> query($query);
        if($result === false) {
            return false;
        }
        while ($row = $result -> fetch_assoc()) {
            $rows[] = $row;
        }
        return $rows;
    }

    /**
     * Fetch the last error from the database
     * 
     * @return string Database error message
     */
    public function error() {
        $connection = $this -> connect();
        return $connection -> error;
    }

    /**
     * Quote and escape value for use in a database query
     *
     * @param string $value The value to be quoted and escaped
     * @return string The quoted and escaped string
     */
    public function quote($value) {
        $connection = $this -> connect();
        return "'" . $connection -> real_escape_string($value) . "'";
    }

    public function getUserIds() {
        $connection = $this -> connect();
        return $this -> select("select distinct user_id from users");
    }
}

【问题讨论】:

  • 请提供Db.php文件的内容
  • 错误消息表明它无法创建 Db 的实例。 var_dump($db);创建后立即产生什么结果?
  • 我同意@num8er,只是指出他/她可能有的其他错误。到目前为止,您怎么看?
  • getUserIds() 在您的班级中不存在,这就是您收到该错误的原因
  • @JonKittell 如果您愿意,我可以通过 Teamviewer 帮助您远程解决此问题。告诉我。

标签: php


【解决方案1】:

在摆弄之后确定,问题是您正在使用自定义 select() 从而使您与标准 sqli_fetch 方法混淆,并且您没有 error_reporting() 开启所以您无法看到错误在哪里来自。希望对您有所帮助。

【讨论】:

    猜你喜欢
    • 2021-06-05
    • 2013-05-19
    • 2012-04-05
    • 1970-01-01
    • 1970-01-01
    • 2015-09-21
    • 2017-07-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多