#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2003
    Posts
    159
    Rep Power
    31

    Lightbulb Switching from mysql to mysqli


    My old class utilizing mysql functions works properly:
    PHP Code:
    class Database {
        var 
    $connection null;
        var 
    $database null;
        function 
    connect($username$password$database$server 'localhost'$port 3306$persistent false) {
            
    $this->connection = empty($persistent) ? mysql_connect("$server:$port"$username$password) : mysql_pconnect("$server:$port"$username$password);
            if (!
    $this->connectionshow_error('servercon'''$username$password$database$server$port);
            
    $this->database mysql_select_db($database$this->connection);
            if (!
    $this->databaseshow_error('dbcon'''$username$password$database$server$port);
            
    $_SESSION['database'] = true;
        }
        function 
    query($sql$connection) {
            
    $result mysql_query($sql);
            if (!
    $resultshow_error('query'$sql$username$password$database$server$port);
            return 
    $result;
            @
    mysql_free_result($result);
        }
        function 
    insert($sql) {
            
    $this->query($sql$this->connection);
        }
        function 
    update($sql) {
            
    $this->query($sql$this->connection);
        }
        function 
    delete($sql) {
            
    $this->query($sql$this->connection);
        }
        function 
    fetch_array($sql$type MYSQL_ASSOC) {
            
    $result $this->query($sql$this->connection);
            while(
    $row mysql_fetch_array($result$type)) {
                
    is_array($array) ? array_push($array$row) : $array = array($row);
            }
            return 
    $array;
            @
    mysql_free_result($array);
        }

    However, I cannot get mysqli to work properly. I have tried so many variations of code.
    PHP Code:
    class Database {
        public static 
    $connection;
        public function 
    connect($username$password$database$server 'localhost'$port 3306) {
            
    $this->connection mysqli_connect($server$username$password$database);
            if (
    $this->connection->connect_errorshow_error('dbcon'''$username$password$database$server$port);
            
    $_SESSION['database'] = true;
        }
        public function 
    disconnect() {
            
    mysqli_close($this->connection);
            
    $this->connection null;
        }
        public function 
    query($sql$connection) {
            
    $result mysqli_query($connection$sql);
            if (!
    $resultshow_error('query'$sql$username$password$database$server$port);
            return 
    $result;
            @
    mysqli_free_result($result);
        }
        public function 
    insert($sql) {$this->query($sql$this->connection);}
        public function 
    update($sql) {$this->query($sql$this->connection);}
        public function 
    delete($sql) {$this->query($sql$this->connection);}
        public function 
    fetch_array($sql$type MYSQL_ASSOC) {
            
    $result $this->query($sql$this->connection);
            while(
    $row mysqli_fetch_array($result$type)) {
                
    is_array($array) ? array_push($array$row) : $array = array($row);
            }
            return 
    $array;
            @
    mysqli_free_result($array);
        }

    It seems that the connection set in the function connect is null when the query functions are called.
  2. #2
  3. No Profile Picture
    I haz teh codez!
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2003
    Posts
    2,547
    Rep Power
    2337
    I would suggest reading the parameters from a config file. Then you can check the $this->connection variable before issuing a query, and if it's null, then use $this->connect.

    It's difficult to help with the specific issue without seeing how exactly you're using the database class otherwise.

    EDIT: Also, if query is a member variable of the class, why are you forcing a connection to be passed in, instead of using the one you have in the class?
    I ♥ ManiacDan & requinix

    This is a sig, and not necessarily a comment on the OP:
    Please don't be a help vampire!
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2003
    Posts
    159
    Rep Power
    31
    I am pulling the connection parameters from a config file. I am checking $this->connection with "if (is_null($this->connection)) die("No DB Connection");" but after removing $connection from the query function parameters and using $this->connection it still does not work. It's working fine on my mysql php v4 code.
    Last edited by Nullified; November 19th, 2012 at 08:21 AM.
  6. #4
  7. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1045
    Hi,

    you cannot access the static property $connection with
    Code:
    $this->connection
    Turn on your error reporting and you'll see two big warnings.

    You either have to make it a normal instance property or use
    Code:
    self::$connection
  8. #5
  9. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2003
    Posts
    159
    Rep Power
    31
    I have tried using public and non public var, static and global. none make a difference.
  10. #6
  11. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,317
    Rep Power
    7170
    I am checking $this->connection with "if (is_null($this->connection)) die("No DB Connection");"
    That line doesn't appear anywhere in the code you posted. What does your current code look like?

    I have tried using public and non public var, static and global. none make a difference.
    Remove the word static. public/private/var makes no difference in this case. global is irrelevant to class variables.


    Change your connect method into your constructor. That way you cannot possibly create a new database object with setting up a connection. My guess is you are forgetting to call your connect method after constructing the object.
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around

IMN logo majestic logo threadwatch logo seochat tools logo