dougan1330 2017-05-04 19:08
浏览 16

如何调试我的PDO数据库类? [重复]

After 10 years away from PHP coding, am getting back in the swing of things going through tutorials. I discovered things have changed, and PDO is the new standard to connect to MySQL.

I set up small projects. I am trying to build my own Class database, and things get all sideways, and I don't figure out why.

So here is my database class:

<?PHP

    $Db = new database; 

    class database
    {
        /////////////
        //Attributs//
        /////////////

        private $db_host;
        private $db_user;
        private $db_pass;
        private $db_name;
        private $bdd;

        /////////////////////
        // internal process//
        /////////////////////

        private function activateDB()
        {
            $this->checkList();
            $this->dbInitilisation();
            $this->dbConnection();
        }

        ////////////////////////
        //Check Websiteconfig//
        ////////////////////////

        private function checkList()
        {
            if(!file_exists('website-config.ini')){
                die('missing configuration file.');
            }
        }

        ///////////
        //Db init//
        ///////////

        private function dbInitilisation()
        {
            // Load config file for database connection info
            $config = parse_ini_file("website-config.ini");

            $this->db_host=$config['db_host'];
            $this->db_user=$config['db_login'];
            $this->db_pass=$config['db_password'];
            $this->db_name=$config['db_name'];
        }

        /////////////////
        //Db connection//
        /////////////////

        private function dbConnection()
        { 
            try
                {
                    $this->bdd = new PDO('mysql:host='.$this->db_host.';dbname='.$this->db_name.';charset=utf8', 'root', '');
                    $this->bdd->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);             
                }
                catch (Exception $e)
                {
                    die('error '.$e);
                }
        }


        public function dbQuery($user_query, $parameters)
        {
            $this->activateDB();

            $query = $this->bdd->prepare($user_query);
            $query->execute($parameters);
            $query->fetch();
            return $query;

        }

    }
?>

and, here is how I am calling it:

$values[':user_id']='1';
$result=($Db->dbQuery('SELECT user_username FROM user WHERE user_id = :user_id',$values));
print_r($result);

When I was not using pdo, and writing my function like normal, I had what I wanted. Now, not any more.

Does anyone has a clue on what I am doing wrong? Do you have any kind of comment on how to improve my overall stuff?

PS: I did read around before coming here (like here: http://php.net/manual/fr/pdostatement.fetch.php (I thought my issue were maybe with the type of fetch, but again, when not in OOP, my code runs ok as it is.) Or here PHP: Database Connection Class Constructor Method but the solution give here is to go on someone else code library, and not allowing to understand what I am actually doing wrong.

</div>
  • 写回答

0条回答 默认 最新

    报告相同问题?