Error with MySql to PDO

I have a problem in converting my current code mysql using PDO, I want to keep the same structure.

This is my MySql structure:

<?php
class Dbase{

    private $_host = "localhost";
    private $_user = "root";
    private $_password = "";
    private $_name = "dbase";

    private $_conndb = FALSE;
    public $_last_query = NULL;
    public $_affected_rows = 0;

    public $_insert_keys = array();
    public $_insert_values = array();
    public $_update_sets = array();

    public $_id;

    public function __construct(){
        $this->connect();
    }

    private function connect(){
        $this->_conndb = mysql_connect($this->_host,$this->_user,$this->_password);
        if(!$this->_conndb){
            die("Database connection failed:<br />".mysql_error());
        }else{
            $_select = mysql_select_db($this->_name,$this->_conndb);
            if(!$_select){
                die("Database selection failed:<br />".mysql_error());
            }
        }
        mysql_set_charset("UTF-8", $this->_conndb);
    }

    public function close(){
        if(!mysql_close($this->_conndb)){
            die("Closing connection failed.");
        }
    }

    public function escape($value){
        if(function_exists("mysql_real_escape_string")){
            if(get_magic_quotes_gpc()){
                $value = stripslashes($value);
            }
            $value = mysql_real_escape_string($value);
        }else{
            if(!get_magic_quotes_gpc()){
                $value = addslashes($value);
            }
        }
        return $value;
    }

    public function query($sql){
        $this->_last_query = $sql;
        $result = mysql_query($sql, $this->_conndb);
        $this->displayQuery($result);
        return $result;
    }

    public function displayQuery($result){
        if(!$result){
            $output  = "Database query failed: ".mysql_error()."<br />";
            $output .= "Last SQL query was: ".$this->_last_query;
            die($output);
        }else{
            $this->_affected_rows = mysql_affected_rows($this->_conndb);
        }
    }

    public function fetchAll($sql){
        $result = $this->query($sql);
        $out = array();
        while($row = mysql_fetch_assoc($result)){
            $out[] = $row;
        }
        mysql_free_result($result);
        return $out;
    }

    public function fetchOne($sql){
        $out = $this->fetchAll($sql);
        return array_shift($out);
    }

    public function lastId(){
        return mysql_insert_id($this->_conndb);
    }

}

I am trying to self resolve but it doesn't work:

<?php
class Dbase{
    private $_pdo_host_dbname = "mysql:host=127.0.0.1;dbname=dbase";

    private $_user = "root";
    private $_password = "";

    private $_conndb = FALSE;
    public $_last_query = NULL;
    public $_affected_rows = 0;

    public $_insert_keys = array();
    public $_insert_values = array();
    public $_update_sets = array();

    public $_id;

    public function __construct(){
        $this->connect();
    }

    private function connect(){
        try{
            $this->_conndb = new PDO($this->_pdo_host_dbname,$this->_user,$this->_password);
        } catch {
            echo 'Connection failed';
        }
    }

    public function escape($value){
        if(get_magic_quotes_gpc()){
            $value = stripslashes($value);
        }
        $value = $this->_conndb->quote($value);
        return $value;
    }

    public function query($sql){
        $this->_last_query = $sql;
        $result = $this->_conndb->query($sql);
        $this->displayQuery($result);
        return $result;
    }

    public function displayQuery($result){
        if(!$result){
            $output  = "Database query failed: ".$this->_conndb->errorInfo()."<br />";
            $output .= "Last SQL query was: ".$this->_last_query;
            die($output);
        }else{
            $this->_affected_rows = $this->_conndb->rowCount($this->_conndb);
        }
    }

    public function fetchAll($sql){
        $result = $this->query($sql);
        $out = array();
        while($row = $this->_conndb->fetch(PDO::FETCH_ASSOC)){
            $out[] = $row;
        }
        return $out;

    }

    public function fetchOne($sql){
        $out = $this->fetchAll($sql);
        return array_shift($out);
    }

    public function lastId(){
        return $this->_conndb->lastInsertId($this->_conndb);
    }
}

Answers


You have but 2 options.

  1. Get rid of this Dbase class of yours and start using raw PDO instead.
  2. Keep with your class without rewriting.

While such a rewriting makes no sense and would be a total waste of time


thank you for your suggestion, my probleme is resolved, on personal help me and explain my how it work:

<?php
class Dbase{
    private $_pdo_host_dbname = "mysql:host=127.0.0.1;dbname=dbase";
    private $_user = "root";
    private $_password = "";

    private $_conndb = FALSE;
    public $_last_query = NULL;
    public $_affected_rows = 0;

    public $_insert_keys = array();
    public $_insert_values = array();
    public $_update_sets = array();

    public $_id;

    public function __construct(){
        $this->connect();
    }

    private function connect(){
        try{
            $this->_conndb = new PDO($this->_pdo_host_dbname,$this->_user,$this->_password);
        } catch (Exception $e) {
            echo 'Connection failed: '.$e->getMessage();
        }
    }

    public function query($sql){
        $this->_last_query = $sql;
        $result = $this->_conndb->query($sql);
        $this->displayQuery($result);
        return $result;
    }

    public function displayQuery($result){
        if(!$result){
            $output  = "Database query failed: ".$this->_conndb->errorInfo()."<br />";
            $output .= "Last SQL query was: ".$this->_last_query;
            die($output);
        }else{
            $this->_affected_rows = $this->_conndb->rowCount($this->_conndb);
        }
    }

    public function fetchAll($sql){
        $result = $this->_conndb->query($sql);
        if ($result) return $result->fetchAll(PDO::FETCH_ASSOC);
    }

    public function fetchOne($sql){
        $out = $this->fetchAll($sql);
        return array_shift($out);
    }

    public function lastId(){
        return $this->_conndb->lastInsertId($this->_conndb);
    }
}

Need Your Help