Page 1 of 1
PHP Error Handling Novice Question
Posted: Mon Mar 02, 2009 9:12 am
by dloomis
Hey there,
I'm pretty new to PHP but I have a few sites that use it. One site I have failed during an insert statement recently for some unknown reason, and because I didn't have proper error handling set up, the code just kept executing the subsequent lines and it just wasn't pretty.
What is an easy way to retrofit error handling in PHP? I'm much more familiar with say Java, where I want to just throw a try{} and catch{} around code. Is there an equivalent in PHP?
In order to show what I have in mind, here's some code snippets:
Code: Select all
$dbh = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
$selected = mysql_select_db("mydb",$dbh)
or die("Could not select first_test");
$sql="insert into ptgames (msgdate,msgbody) values ('" . $dt . "','" . $mg . "')";
mysql_query($sql);
...other code...
I want to test to see if the mysql_query($sql); was successful with as little rework as possible. How do I do that?
Thanks for any assistance!
Re: PHP Error Handling Novice Question
Posted: Mon Mar 02, 2009 9:30 am
by Mark Baker
dloomis wrote:What is an easy way to retrofit error handling in PHP? I'm much more familiar with say Java, where I want to just throw a try{} and catch{} around code. Is there an equivalent in PHP?
Funny you should say that
PHP just happens to have
try/catch
Re: PHP Error Handling Novice Question
Posted: Mon Mar 02, 2009 10:03 am
by dloomis
Mark Baker wrote:dloomis wrote:What is an easy way to retrofit error handling in PHP? I'm much more familiar with say Java, where I want to just throw a try{} and catch{} around code. Is there an equivalent in PHP?
Funny you should say that
PHP just happens to have
try/catch
That does make it easy, thanks! I didn't know PHP supported that. So if it's not able to insert for any reason, will it throw an exception?
New code:
Code: Select all
try{
$sql="insert into ptgames (msgdate,msgbody) values ('" . $dt . "','" . $mg . "')";
mysql_query($sql);
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
echo 'SQL is: ' . $sql . "\n";
die("Unable to send email. Contact Admin");
}
Re: PHP Error Handling Novice Question
Posted: Mon Mar 02, 2009 10:29 am
by highjo
but you should bear in mind that when using try and catch on function like mysql_connect (basically any connection stuff depending on the php version you have) you will always have errors.forgot exactly what happens at that time but it seems you have to set your error reporting.You should try the PDO layer
http://www.phpro.org/tutorials/Introduc ... P-PDO.html
Re: PHP Error Handling Novice Question
Posted: Mon Mar 02, 2009 10:37 am
by dloomis
highjo wrote:but you should bear in mind that when using try and catch on function like mysql_connect (basically any connection stuff depending on the php version you have) you will always have errors.forgot exactly what happens at that time but it seems you have to set your error reporting.You should try the PDO layer
http://www.phpro.org/tutorials/Introduc ... P-PDO.html
Is there a better way then?
Re: PHP Error Handling Novice Question
Posted: Mon Mar 02, 2009 10:38 am
by Mark Baker
highjo wrote:You should try the PDO layer
Which also means catching PDOException
Re: PHP Error Handling Novice Question
Posted: Mon Mar 02, 2009 11:26 am
by mattpointblank
What's the advantage to doing it this way? Like, say, for form validation, I just run a bunch of tests on input, and only process it if it passes all of those checks. If there's an error, I send the user back to the form. How would this help me?
Re: PHP Error Handling Novice Question
Posted: Mon Mar 02, 2009 11:52 am
by highjo
well mattppointblank there is differents ways of doing that depending on the user experience you want to give.
1 the easiest way is to have an error page(this is just a replication of ASP.NET module).so you do your validation and want to send the info to the database.if the connection fails or something happened then the PDOException would catch it and there like this
Code: Select all
<?php
try{/// your codes validation and inserting into the database}
catch(PDOException $e){ header("Location:errorpage.php?message=".$e->getMessage());
//please remember that the code $e->getMessage() is for you.on production you can simly set message=Error occured or something
//you have to know that you should use some pdo attribute before using this. its used with the function PDO::ATTR_ERRMODE and PDO::ERRMODE_EXCEPTION //please check php.net for that
}
?>
2.it's more complex.you can use session to store temporary what is entered into the form and return the user to the form with those.
hope didn't confuse you more.
Re: PHP Error Handling Novice Question
Posted: Mon Mar 02, 2009 2:12 pm
by mickeyunderscore
The easiest way to do this is to check the return from the mysql_query() function, as it returns a boolean false if it fails. Since PHP kindly typecasts everything for you, it is possible to do a simple boolean check, so even if your query returns a mysql resource, it will typecast to a boolean and equal true. e.g.
Code: Select all
if( mysql_query($sql) ){
//query worked
}else{
//query didn't work
}