0

I am trying to use PDO statements in my classes. I am new to oop and I am trying to figure out how it is supposed to be done. I looked at some of the examples on this site, and used their logic but none of them seemed to work. There were no fatal errors, or warnings in the source code but the sql statement was never executed. So I feel I'm just missing one small thing that I haven't learned yet.

So far what I have learned is that the PDO connection has to be made in a class and should be called via the constructor method. I got the code for the database class from someone else.

class Database
{
    protected static $Connection;

    public static function Connect()
    {
        if((self::$Connection instanceof PDO) === false)
        {
            $dbhost = 'host';
            $dbname = 'name';
            $dbuser = 'user';
            $dbpass = 'pass';

            self::$Connection = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);
        }
        return self::$Connection;
    }
}

And this is just the constructor and an example a PDO statement in one of the functions of the class

class blahJR extends blah
{       
    private $Database;

    function __construct ($sUser)
    {
        parent::__construct($sUser);

        //Updated below: $this->Database = Database::Connect();
        $this->Database = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass); //All the variables are given and the connection is definitely being made
    } //end __construct

public function foo($sString)
{
    try 
    {
         $Statement = $this->Database->prepare("INSERT INTO table (column) VALUES (?)");
         $Statement->execute(array($sString));
    }
    catch (PDOexception $e)
    {
         DatabaseError($e);
    }
}

Any help would be greatly appreciated. Thank you for your time.

Updated: I added the function around the try statement to make it more clear, it was always there, I just never added it when I made the question. I hope that clarifies it. It doesn't report any errors still. I have no idea why this shouldn't work.

1 Answers1

2

There is no reason to make a separate class to connect to a database. Just create an instance of PDO and inject it into your classes / methods that need it.

Also note that that Database class has not much to do with OOP.

class BlarJR extends blah
{
    private $connection;

    public function __construct(\PDO $connection, $user)
    {
        parent::__construct($user);

        $this->connection = $connection;
    }

    public function foo($someValue)
    {
        $stmt = $this->connection->prepare("INSERT INTO table (column) VALUES (?)");
        $stmt->execute(array($someValue));
    }
}

$connection = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);
$connection->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$blarJR = new blahJR($connection, $user);

try {
    $blarJR->foo('my value');
} catch (\PDOexception $e) {
    echo $e->getMessage();
}

As you can see I have also enabled exceptions on the PDO connection as well as disabled emulated prepared statements. You are currently trying to catch exception which will not be thrown. By default PDO uses PDO::ERRMODE_SILENT:

This is the default mode. PDO will simply set the error code for you to inspect using the PDO::errorCode() and PDO::errorInfo() methods on both the statement and database objects; if the error resulted from a call on a statement object, you would invoke the PDOStatement::errorCode() or PDOStatement::errorInfo() method on that object. If the error resulted from a call on the database object, you would invoke those methods on the database object instead.

What you currently have will produce errors, because you have stuff just sitting in a class without being in a method.

Parse error: syntax error, unexpected 'try' (T_TRY), expecting function (T_FUNCTION) in...

Community
  • 1
  • 1
PeeHaa
  • 71,436
  • 58
  • 190
  • 262
  • I agree. It also looks like he was attempting to create a `Singleton` with the `Database` class, but that is unnecessary with connection pooling. – crush Jan 23 '13 at 20:17
  • I'm sorry I should have been clearer, It was in a method, I didn't just leave it blank. I'll update my question to show you what it really looked like. –  Jan 23 '13 at 20:32
  • @user1928545 As I stated. Enable exceptions on the connection to see what is going on or manually call `$stmt->errorInfo()`. Also I'm not sure whether you are trying to implement a singleton (looks like it), but the singleton pattern has [no place in OOP](http://stackoverflow.com/a/11923384/508666). – PeeHaa Jan 23 '13 at 20:39
  • I enabled exceptions, and you were right. The string wasn't being added because it was null. I've got it working now. Thank you for the help. –  Jan 23 '13 at 21:21