Forum Moderators: coopster

Message Too Old, No Replies

Question about using "or die();" after mysql query in php.

         

nelsonm

6:20 pm on Nov 21, 2010 (gmt 0)

10+ Year Member



Hi all,

I have noticed in some php scripts, some programmers us either of the following:

$Result = mysql_query($sql, $conn) or die ("<br>** Error in database table <b>".mysql_error()."</b><br>$sql");


others will code:
$Result = mysql_query($sql, $conn);

if (!$Result){
echo "<br>** Error in database table <b>".mysql_error()."</b><br>$sql";
}


1. Is using "or die()" better than using "if(!$Result)"?

2. Is using "or die()" then testing for "if(!Result)" as shown below redundant?

$Result = mysql_query($sql, $conn) or die ("<br>** Error in database table <b>".mysql_error()."</b><br>$sql");

if (!$Result){
echo "<br>** Error in database table <b>".mysql_error()."</b><br>$sql";
}

Matthew1980

7:13 pm on Nov 21, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there nelsonm,

Lot's of books give you this as the way to do it, either will function, just the way as it is layed out. The only thing as I can offer to this is this: Only really need to use this whilst developing your site, when you have the kinks ironed out, remove those lines, as this can reveal a lot about the structure of your DB, consequently, hackers will have more information to cause harm to your site, and there is is the issue of traffic using this method, your not really keeping the user 'interested' in your site.

If you insist on having the mysql error function in use, at least use a piping method so that that you can log the error's to a text file should anything go wrong. At least then you can have a 'holding' page so that users can see that your site is experiencing an error - have a nice try again later message displayed, with a link to the homepage, but at least this way it *kinda* keeps the traffic there, sometimes mysql can just be down for a couple of mins, always good to have that option there.

Cheers,
MRb

rocknbil

4:54 pm on Nov 23, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



They have two distinctly different purposes. If $result tests for a result returned from a successful query, "do this or die()" tests any internal function for a successful execution. You could/should use both as you surmised, and maybe another to test for rows in the result.

You can also make use of internal error handlers for debugging, but should remove them in deployment for security considerations.

$dev=1; // set to 0 on deployment

if ($dev == 1) {
$result = mysql_query($query) or die("could not execute: $query " . mysql_error());
}
else { $result = mysql_query($query) or die("could not get product list"); }

An important part of the previous that will save you time: make all the messages unique so you know right where to find the problem.

"could not get product list"
"could not insert new product"
"could not delete product option"

A more robust system would have your messages mapped in some way so they can be modified without digging through the code.

$db_errors = Array (
'prod_query' => "could not get product list",
'prod_insert' => "could not insert new product",
'del_prod' => "could not delete product option"
);

$result = mysql_query($query) or die($db_errors['prod_query']);

Personally I've never found testing $result to be all that useful, I'm more interested in what's in it. If the query executes, surely there will be a result, if it's null, this tests for expected contents just as well - and better yet, can be used for a third level, programming logic.

else { $result = mysql_query($query) or die("could not get product detail"); }
if ($row = mysql_fetch_array($result)) {
// output product detail
}
else {
echo "<p>No details for this product.</p>";
}

I usually do things a little differently and rarely use mysql_num_rows. I execute a count, and based on the count execute the actual query. So for multiple rows, most would do something like

if (mysql_num_rows($result) > 0) {
// while loop for products
}
else {
echo "<p>No details for this product.</p>";
}

This simplifies your programming a little by using the results to form the logic of your output.

nelsonm

7:55 pm on Nov 23, 2010 (gmt 0)

10+ Year Member



rocknbil,

Thanks for your insite.

I'm just learning to deal with php and mysql as part of a job i'm trying to complete. Ironically, even though i set $Results as well, i too don't use it much in, as you say, setting the logic of the code.