Question

I'm not trying to create my own PDO class, just extend it so I can insert a try catch statement in my own execute function (myexecute) so I do not have to write the code every time.

Firstly, is this a good ideas? or should I just scrap it?

so I want this:

$DB = new Database ( HOST, DB, USER, PASS );
$query = $DB->prepare("INSERT * FROM books WHERE title = :title");
$query->bindParam( ':title', $_POST['title'] );
$query->myexecute();

if there was a problem my function (myexecute) would sort the error handling the problem is Im getting this error:

Fatal error: Call to undefined method PDOStatement::myexecute()

Any ideas what Im doing wrong?

 class Database extends PDO
    {
    private $dbh;
    private $error;
    private $total;
    private $p_query;


    public function __construct($hostname,$dbname,$username,$password)
    {
        try  
        { 
            $dsn = 'mysql:host='.$hostname.';dbname='.$dbname;
            parent::__construct($dsn, $username, $password);
        } 
        catch(PDOException $e)
        {
            echo "DataBase Error: Connection error.<br>".$e->getMessage();
            exit;
        }
    } 

    public function myexecute()
    {
        try 
        {
            return parent::execute();
        } 
        catch (PDOException $e) 
        {
            echo "DataBase Error: The user could not be added.<br>".$e->getMessage();
            exit;
        }   
        catch (Exception $e) 
        {
            echo "General Error: The user could not be added.<br>".$e->getMessage();
            exit;
        }
    }
}
Was it helpful?

Solution

You don't have to write try catch statement with raw PDO either.

So, the reason you are trying to extend PDO for is a false one.

Just quit that bad habit of writing try catch on every statement and you'll be fine with raw PDO.

OTHER TIPS

To do what you are trying to do, you'd have to write a wrapper around PDOStatement and override the prepare method in your Database class to return that wrapper of PDOStatement. So that you can have a method myexecute in your subclass of PDOStatement.

Wanting to extend PDOStatement is quite common and is supported by PDO. You just need to specify which class to use for statements.

class DBIStatement extends \PDOStatement
{
    public function fetchrow_hash()
    {
        $rows = $this->fetchAll();
        return (count($rows) == 1) ? new DBIDataRow($rows[0]) : new DBIDataRow(array());
    }
}
class DBI
{
    public $dbh;

    public function __construct($type, $host, $name, $user, $pass)
    {   
        $dsn = sprintf('%s:host=%s;dbname=%s',$type,$host,$name);
        $this->dbh = new \PDO($dsn,$user,$pass,array(
            \PDO::ATTR_ERRMODE,           PDO::ERRMODE_EXCEPTION,
            \PDO::ATTR_DEFAULT_FETCH_MODE,PDO::FETCH_ASSOC,
        ));
        // *** This is what tells PDO to use your statement class.
        $this->dbh->setAttribute(\PDO::ATTR_STATEMENT_CLASS,array('DBIStatement'));
    }

Don't be alarmed by the back slashes. This is part of a namespaced application.

The only strange thing is that I could not pass the statement class in the pdp constructor like I could wit the other attributes. But is does work.

No need to fool with the pdo prepare method at all.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top