Juli's dumb question of the day
Date: 01/12/09
(PHP Community) Keywords: mysql, database, sql
Hi everyone! Time for another one of my dumb questions!
The short version: Why do my try/catch blocks not seem to catch properly?
I'm using MySQL to update a recipe database I'm creating (and have been creating for just about forever. It's one of those projects that you can only pick up in your very limited spare time) and just for grins, I wanted to test and see if the try/catch blocks I set up to catch MySQL errors in host connection, database connection, and query running worked.
So I changed the password to see if they would work. Let me tell you, it was a big pile of fail and MySQL errors.
In theory, if the connection to the host is unsuccessful, it should say "Problem connecting to the host" and give the info stored in mysql_error(). But it doesn't. It just plugs along and fails like the try/catch blocks weren't even there. And I'm not quite sure why.
So, what have I done to Eff up my Try/catch blocks?
Example code...
public function create_recipe()
{
try
{
$create_connect = $this->_db_connect();
try
{
$this->recipe_id = $this->_set_id($create_connect, "recipes");
//Selects the database from the server.
$db_select = mysql_select_db("recipes", $create_connect);
try
{
//Query to create the recipe in the database.
$query = "INSERT INTO recipes ";
$query .= "VALUES(" . $this->recipe_id . ", '" .
$this->recipe_info["recipeName"] . "', ";
$query .= "'" . $this->recipe_info["numServings"] . "', '" .
$this->recipe_info["prepTime"] . "', ";
$query .= "'" . $this->recipe_info["cookTime"] . "', NULL, '" .
$this->recipe_info["origAuthor"] . "', ";
$query .= "'" . $this->recipe_info["origLocation"] . "', '" .
$this->recipe_info["notes"] . "', NULL);";
//Timestamp is automatically created in the database upon insert.
//Runs $query on the database. Stops the script if there is an error.
$result = mysql_query($query, $create_connect);
mysql_close($create_connect);
} //end try $eee
catch (Exception $eee)
{
echo "Problem running the query. " . mysql_error($create_connect);
echo "
";
mysql_close($create_connect);
} //end catch $eee
} //end try $ee
catch (Exception $ee)
{
//Verifies that the database/table connection was successful. If not, it kills the
//application.
echo "Problem selecting the table. " . mysql_error($create_connect);
echo "
";
mysql_close($create_connect);
} //end catch $ee
} //end try $e
catch (Exception $e)
{
//Verifies that the connection to the host was successful. If not, it kills the
//application.
echo "Problem connecting to the host. " . mysql_error();
echo "
";
} //end catch $e
} //end create_recipe()
private function _db_connect()
{
$hostname = "********";
$username = "********";
$password = "********";
return mysql_connect($hostname, $username, $password);
} //end db_connect()
Source: http://community.livejournal.com/php/652844.html