0

I have a php/mysql database class as in below code. I am failing to get any mysql error from the error method when there is an error in the mysql query. guide me if anything wrong here. if i used non oop method to query same thing, then i can get the expected error from that. but nothing from this class.

class db {
    protected static $connection;

    var $hostname;
    var $username;
    var $password;
    var $database;

    public function __construct() {
        $this->hostname = "localhost";
        $this->username = "root";
        $this->password = "";
        $this->database = "my_db";
    }

    public function connect() {    
        if(!isset(self::$connection)) {               
            self::$connection = new mysqli($this->hostname,$this->username,$this->password,$this->database);
        }

        // 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;
        }

        self::$connection -> query("SET NAMES 'utf8'");

        return self::$connection;
    }


    public function query($query) {
        // Connect to the database
        $connection = $this -> connect();
        // Query the database
        $result = $connection -> query($query);

        return $result;
    }

    public function multi_query($query){
        // Connect to the database
        $connection = $this -> connect();

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

        return $result;
    }

    public function insert($query) {
        // Connect to the database
        $connection = $this -> connect();

        // Query the database
        $connection -> query($query);
        // Get inserted id
        $insertid = $connection -> insert_id;

        return $insertid;
    }

    public function select($query) {
        $rows = array();
        $result = $this -> query($query);
        if($result === false) {
            return false;
        }
        while ($row = $result -> fetch_assoc()) {
            $rows[] = $row;
        }
        return $rows;
    }

    public function num_rows($query) {
        $result = $this -> query($query);

        if($result === false) {
            $count = 0;
        }
        else $count = $result->num_rows;

        return $count;
    }

    /**
     * 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 escape($value) {
        $connection = $this -> connect();
        return $connection -> real_escape_string(trim($value));
    }
}

i am expecting the error from this method. but i am not getting any error when there is an error from mysql query.

public function error() {
    $connection = $this -> connect();
    return $connection -> error;
}

Below example table has, id (Auto Increment), and three other fields VARCHAR (3). If I run below php, no error details coming, even though there's an obvious mismatch between the number of columns in the respective parts of the query...

    <?php
    include('class.db.php');

    $query = "INSERT INTO abc_table (`a`,`b`,`c`) VALUES ('a','b','c','d')";
    $insert_id = $db->insert($query);
    if($insert_id>0){
        echo "Insert Success !";
    }
    else echo "Insert Failed. ".$db->error();
    ?>
Strawberry
  • 33,750
  • 13
  • 40
  • 57
LahiruTM
  • 638
  • 4
  • 16
  • Side note; it's great to see a decent OOP'er underway, you should pick up PSR-1/PSR-2 just to make it shine. Also, you can just `$this->connect()->real_escape_string(trim($value))` no point having the redundant variable set – zanderwar Feb 29 '20 at 04:25
  • `if(self::$connection === false) {` -> `if (self::$connection->connect_error)` then you can check `self::$connection->connect_errno` to give finer granularity reasons, for example 1049 means connection was successful, but the database does not exist – zanderwar Feb 29 '20 at 04:27
  • `$query = self::$connection->query(....)` then `if(!$query) { throw new \RuntimeException('DB: ' . self::$connection->error); }` – zanderwar Feb 29 '20 at 04:29
  • @zanderwar : is it possible to get this error in my error() method ? Actually i want to get the last db error. – LahiruTM Feb 29 '20 at 05:52
  • 1
    Does this answer your question? [mysqli\_fetch\_assoc() expects parameter / Call to a member function bind\_param() errors. How to get the actual mysql error and fix it?](https://stackoverflow.com/questions/22662488/mysqli-fetch-assoc-expects-parameter-call-to-a-member-function-bind-param) – Dharman Feb 29 '20 at 08:59

2 Answers2

1

I could managed to resolve the problem by adding this to error() method in my class file.

return self::$connection -> error;

So, my error method is now

public function error() {
    return self::$connection -> error;
}
LahiruTM
  • 638
  • 4
  • 16
0

Try to put this code on your .htaccess file

php_flag display_startup_errors on
php_flag display_errors on
Murtaza JAFARI
  • 674
  • 7
  • 10