0

I have a method named getUserIds in a class called Db.php.

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

When I call the function in a file located in the same folder as Db.php I get

PHP Fatal error: Call to undefined function getUserIds()

The file generating the error looks like this:

<?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>';
?>

Here is the contents of 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");
    }
}
24
  • plz provide content of Db.php file Commented Aug 24, 2015 at 18:44
  • 2
    Error message suggests that it could not create an instance of Db. What is the result of var_dump($db); immediately after creating it? Commented Aug 24, 2015 at 18:45
  • 1
    I agree @num8er, just pointing out other errors that he/she may have. What do you think so far? Commented Aug 24, 2015 at 18:48
  • 2
    getUserIds() does not exist in your class, that is why youre getting that error Commented Aug 24, 2015 at 18:50
  • 1
    @JonKittell If you want I can help you troubleshoot this remotely through Teamviewer. Let me know. Commented Aug 24, 2015 at 19:08

2 Answers 2

1

OK after fiddling around, the problem is that you are using a custom select() thus confusing you with the standard sqli_fetch method, and you don't have error_reporting() on so you werent able to see where the error came from. Hope all that helped.

Sign up to request clarification or add additional context in comments.

Comments

0

I ran into this exact problem, but it was because I failed to include "$this->" to point to the function in the same file.

EG:

$Ts = CalculateTemperatureOffset($T); //fails with error

$Ts = $this->CalculateTemperatureOffset($T); //works

Hopefully this helps other people who find run into something similar.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.