PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Mon Jun 24, 2019 5:30 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: PDO & fatal errors
PostPosted: Tue Dec 24, 2013 1:29 pm 
Offline
DevNet Master
User avatar

Joined: Sun Feb 15, 2009 12:08 pm
Posts: 2794
Location: .za
I'm using PDO to connect to a database, handle database interactions and i'm not sure how to handle the errors (fatal) that result when no connection to a database can be established.

Syntax: [ Download ] [ Hide ]
<?php
 $dbh = new PDO('mysql:host=localhost;dbname=est', $user, $pass);
 // meanwhile somewhere else in the code
 $countSql = "SELECT COUNT(`id`) FROM `user_main` WHERE
 `username` = ?"
;
 $sth = $dbh->prepare($countSql);
?>


If there is an error connecting to the database then no PDO object is created which causes a fatal error when using for instance prepare()

Fatal error: Call to a member function prepare() on a non-object in...

My thinking is to check whether $dbh is an object, and based on that continue with the process, something like

Syntax: [ Download ] [ Hide ]
<?php
 if (is_object($dbh)) {
   // i can go on and interact with the database
 }
?>


Is this approach correct or should i be looking at something different, and if so, what would you recommend?

Some more information that might be relevant - i'm creating an instance of a PDO object inside the construct of a class which i then set as class property so most of my checking would be done inside other class methods.

_________________
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering


Top
 Profile  
 
 Post subject: Re: PDO & fatal errors
PostPosted: Tue Dec 24, 2013 3:09 pm 
Offline
Moderator
User avatar

Joined: Mon Nov 03, 2003 7:13 pm
Posts: 5978
Location: Odessa, Ukraine
Docs say that failure to establish database connection will always result in exception. Could it be that you're catching and then ignoring the exception caught?


Top
 Profile  
 
 Post subject: Re: PDO & fatal errors
PostPosted: Tue Dec 24, 2013 4:59 pm 
Offline
Moderator
User avatar

Joined: Tue Nov 09, 2010 3:39 pm
Posts: 6425
Location: Montreal, Canada
If your class depends on PDO, I'd catch the PDO exception and throw it again. No point instantiating a class you can't use.

_________________
Supported PHP versions No longer supported versions


Top
 Profile  
 
 Post subject: Re: PDO & fatal errors
PostPosted: Tue Dec 24, 2013 8:41 pm 
Offline
Moderator
User avatar

Joined: Tue Nov 09, 2010 3:39 pm
Posts: 6425
Location: Montreal, Canada
Better still, require it as a constructor argument. Creating the PDO object shouldn't be this class's responsibility.

_________________
Supported PHP versions No longer supported versions


Top
 Profile  
 
 Post subject: Re: PDO & fatal errors
PostPosted: Wed Dec 25, 2013 6:17 pm 
Offline
DevNet Master
User avatar

Joined: Sun Feb 15, 2009 12:08 pm
Posts: 2794
Location: .za
Weirdan wrote:
Could it be that you're catching and then ignoring the exception caught?

I don't understand what you're saying here; i do catch the exception but the ignoring part? Below is the code that i use when creating the PDO instance.

Syntax: [ Download ] [ Hide ]
<?php
try {
        $dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass);                                      
}
catch (Exception $e) {
        // Better message.
        echo 'Cannot establish connection to database';
        // ^ this displays when i alter the connection details
}
?>


Celauran wrote:
Creating the PDO object shouldn't be this class's responsibility.

Sounds like a better option than what i am currently doing, how would i then check if the argument passed was indeed a PDO object? Would is_object() suffice or type hinting?

_________________
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering


Top
 Profile  
 
 Post subject: Re: PDO & fatal errors
PostPosted: Wed Dec 25, 2013 11:13 pm 
Offline
Moderator
User avatar

Joined: Tue Nov 09, 2010 3:39 pm
Posts: 6425
Location: Montreal, Canada
I'd use type hinting over is_a. is_object is too vague, IMO.

_________________
Supported PHP versions No longer supported versions


Top
 Profile  
 
 Post subject: Re: PDO & fatal errors
PostPosted: Thu Dec 26, 2013 5:33 am 
Offline
DevNet Master
User avatar

Joined: Sun Feb 15, 2009 12:08 pm
Posts: 2794
Location: .za
thanks for the suggestion :)

_________________
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group