0

I get an error when i go to index.php:

Fatal error: Call to undefined method DB::quote() in C:\xampp\htdocs\barafranca.nl\config\main_functions.php on line 10

The weird thing is that when i test this (php code on index.php) in the main_functions.php everything works as it should. But when include main_functions.php on my index.php page, and use the getAccountQuery function, it can't find the methhods of my Db() object anymore.

PHP_VERSION: 5.5.30.

Db.php:

<?php 
class Db {
    // The database connection
    protected static $connection;

    /**
     * Connect to the database
     * 
     * 
     */
    public function connect() {    
        // Try and connect to the database
        if(!isset(self::$connection)) {
            self::$connection = mysqli_connect('localhost', 'root', '', 'bfr');
        }

        // 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 mysqli_connect_error();
        }
        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) . "'";
    }
}
?>

main_functions.php

<?php
include("Db.php");

function getAccountQuery($username)
{
    // Database object
    $db = new Db();    

    // Quote and escape parameter value
    $username = $db -> quote($username);
    $sql = "SELECT * FROM `users` WHERE username=". $username;
    $result = $db -> select($sql);

    // If result matched $username, table row must be 1 row

    if(!empty($result)) 
    {
        echo "NOT EMPTY";
        return $result[0];
    }else {
        return null;
    }
}

function getOnlineUsersCount($con)
{
    $usersOnlineNow = $con->query("SELECT * FROM users WHERE online >= NOW()-500");
    return $usersOnlineNow->num_rows;
}
?>

index.php

<?php
include("config/config.php");
include("config/main_functions.php");

session_start();

$test = getAccountQuery("Nick");
echo $test;

?>

config.php

<?php
$siteName = "CrimeGame";
$siteDomain = "CrimeGame.nl";
$siteMail = "admin@CrimeGame.nl";
?>

DIRECTORY INFO: DIR

NickGames
  • 312
  • 1
  • 18

0 Answers0