在课堂上使用PDO和php和mysql

i am trying to use PDO in php using classes.

so i have defined one config file like that

<?php
global $configVars;

    $configVars['online'] = false;  

    if(($_SERVER['SERVER_NAME'])!='localhost' and ($_SERVER['SERVER_NAME'])!='abc')
    {
$configVars['dbhost']       = "localhost";      
$configVars['dbuser']       = "dbuser";   
$configVars['dbpassword']   = "dbpass";     
$configVars['dbname']       = "dbname"; 

}
?>

then i have defined class to connect the database

<?php
class dbClass{
var $dbHost,
      $dbUser,
        $dbName,
          $dbPass,
            $dbTable,
              $dbLink,
                $resResult,
                  $dbh,
                  $dbPort;  


        function dbClass(){
            global $configVars;
            $this->dbHost = $configVars['dbhost'];
            $this->dbUser = $configVars['dbuser'];
            $this->dbPass = $configVars['dbpassword'];
            $this->dbName = $configVars['dbname'];
            $this->connect_PDO();
        }

        function connect_PDO(){
            try {
            $dbh = new PDO('mysql:host='.$this->dbHost.';dbname='.$this->dbName.'', $this->dbUser, $this->dbPass);
            $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            echo "Connected.";
            }
            catch(PDOException $e) {
            echo "I'm sorry Charlie, I'm afraid i cant do that.";
            file_put_contents('PDOErrors.txt', $e->getMessage(), FILE_APPEND);
            $dbh = null;
            }
        }
 }       

?>

now i have defined another class to select the records from table

<?
class cms extends dbClass
{

function get_rec($id=0)
    {
        ($id==0?$addQuery="":$addQuery=" where id =".$id);
        $statement = $dbh->prepare("select * from TABLENAME :name order by id");
        $statement->execute(array(':name' => $addQuery));
        $row = $statement->fetchAll(); 
        return $row ;
    }   


}
?>

now when i am using this function in my PHP file like this

<?php 
$objCMS=new cms();
$getCMS=$objCMS->get_rec(1);
echo $getCMS[0];
?>

i got following out put

Connected.
Fatal error: Call to a member function prepare() on a non-object in /Applications/XAMPP/xamppfiles/htdocs

i am trying to sort this out from last 2 days but no success.

i am new to PDO and want to use it.

Thanks

You need to call dbClass() in your constructor for cms

add this to the cms class

function cms() {
    parent::dbClass();
}

and change get_rec to have:

$statement = $this->dbh->prepare("select * from TABLENAME :name order by id");

when using $dbh you need to reference it as $this->dbh

You need to make the $dbh to $this->dbh changes in you need to update connect_PDO() also

You $dbh is declared in the base class but without a specific scope I think it's going to default to 'private'. Hence not visible in the sub class.

Also you aren't calling the constructor. Add a call to 'parent::__construct()' in your sub class. http://php.net/manual/en/language.oop5.decon.php

You are going in wrong direction about solving this one.

If you want to give some object access to database, then that object needs access to database object that provides methods to interact with the database. You got that right.

But, inheritance is "IS A" relationship. And your CMS object is not a database type of object, and doesn't have same function as database object right? CMS object should just use Database and this is called composition. This relationship is also called "HAS A" relationship.

So instead of using Inheritance here, you should use Composition like this:

class CMS
{
    protected $db;
    // other CMS related stuff
}

$db = new PDO($dsn, $username, $password, $options);
$cms = new User($db);

This way your CMS can use database without using inheritance.

  • Google about composition vs. inheritance and when to use it.
  • Also take a look at this answer here: PHP Mysqli Extension Warning
  • And read this article and go through examples to understand why composition is a way to go when providing some class a way to interact with database.

I know this is not an answer to your question, but you have gone in the wrong direction to solve your problems. I see lots of people use only inheritance so just be aware that there is much more then inheritance to make your objects interact. And in this situation, its just wrong to use Inheritance since your CMS object has nothing to do with Database, they are not the same "type". CMS should only use Database.

Hope this helps!