Converting my lightweight MySQL DB wrapper into MySQLi. Pesky Problems

Posted by Chaplin on Stack Overflow See other posts from Stack Overflow or by Chaplin
Published on 2012-09-10T14:12:46Z Indexed on 2012/09/10 15:38 UTC
Read the original article Hit count: 150

Filed under:
|
|
|
|

Here is the original code: http://pastebin.com/DNxtmApY. I'm not that interested in prepared statements at the moment, I just want this wrapper updating to MySQLi so once MySQL becomes depreciated I haven't got to update a billion websites.

Here is my attempt at converting to MySQLi.

<?

$database_host  = "127.0.0.1";
$database_user  =  "user";
$database_pass  =  "pass";
$database_name  = "name";

$db = new database($database_host, $database_user, $database_pass, $database_name);

class database {

    var $link, $result;

    function database($host, $user, $pass, $db) {
        $this->link = mysqli_connect($host, $user, $pass, $db) or $this->error();
        mysqli_select_db($db, $this->link) or $this->error();
    }
    function query($query) {
        $this->result = mysqli_query($query, $this->link) or $this->error();
        $this->_query_count++;
        return $this->result;
    }
    function countRows($result = "") {
        if ( empty( $result ) )
            $result = $this->result;
        return mysqli_num_rows($result);
    }
    function fetch($result = "") {
        if ( empty( $result ) )
            $result = $this->result;
        return mysqli_fetch_array($result);
    }
    function fetch_num($result = "") {
        if ( empty( $result ) )
            $result = $this->result;
        return mysqli_fetch_array($result, mysqli_NUM);
    }
    function fetch_assoc($result = "") {
        if ( empty( $result ) )
            $result = $this->result;
        return mysqli_fetch_array($result, mysqli_ASSOC);
    }
    function escape($str) {
        return mysqli_real_escape_string($str);
    }
    function error() {
        if ( $_GET["debug"] == 1 ){
            die(mysqi_error());
        } else {
            echo "Error in db code";
        }
    }
}



function sanitize($data) {

    //apply stripslashes if magic_quotes_gpc is enabled
    if(get_magic_quotes_gpc())
        $data = stripslashes($data);

    // a mysqli connection is required before using this function
    $data = trim(mysqli_real_escape_string($data));

    return $data;

}

However it chucks all sorts of errors:

Warning: mysql_query(): Access denied for user 'www-data'@'localhost' (using password: NO) in /home/count/Workspace/lib/classes/user.php on line 7 Warning: mysql_query(): A link to the server could not be established in /home/count/Workspace/lib/classes/user.php on line 7 Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in /home/count/Workspace/lib/classes/user.php on line 8 Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, object given in /home/count/Workspace/lib/classes/database.php on line 31

© Stack Overflow or respective owner

Related posts about php

Related posts about mysql