no error retuns in the mysql error method

LahiruTM :

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();
    ?>
LahiruTM :

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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=19508&siteId=1