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...
Thanks for any assistance!