Jump to content

mysql statement OR ....


Recommended Posts

Alright since I wouldn't know where to begin to look for this ill admit I am hitting the form first.

I don't want to muck it up my work, more than I have at least. So that said when one makes a update query or insert or connection.. whatever to mysql, via PHP its customary to have a "or die(...);" after the query in case anything fails. What I want to know is can I have something else, instead of a "or die(...);"? Could I put a variable there instead or anything?


example instead of using:

$update = mysql_query("UPDATE thisthat SET $foo='$bar' WHERE id='$ID'") or die('Error: '.mysql_error().');


could I do something like:

$update = mysql_query("UPDATE thisthat SET $foo='$bar' WHERE id='$ID'") or $var1 = "whoops"; $var1msg = "Query Failed: update thisthat";


Link to comment
Share on other sites

Because or anything() does not address error recovery (what your code does when an error occurs), blindly continue execution, return a known/default value, output a useful error message and contain with the remainder of the presentation logic on the page, it really should not be used except for debugging purposes.


Unless you want to use exceptions, all you really need to do is just use conditional logic so that you can address error recovery when an error occurs - http://www.phpfreaks.com/forums/index.php/topic,280845.msg1330662.html#msg1330662

Link to comment
Share on other sites

OK, I am understanding the error catching. Though I am not trying to catch the errors per say. What I am trying to do is or want to know if possible is, can I do something else rather than pretty much for an exit of or kill the script. I have a custom error handler I have been working on cause of the way I am implementing AJAX style coding. Which the custom error handling works fine up until a sql query breaks. at which point like someone else pointed out. The code outside of the SQL still runs with default parameters. What I want to do is instead of using DIE or the method from the article link above is set a variable so I can trigger my error handler, problem is if its not possible I dont want to spend all day trying to make it work to find out that I can't then figuring out it cant work and having to go back over everything fixing it to work like it was prior to messing with it like I'm intent on.

Link to comment
Share on other sites

I'm thinking that an explanation of what using or anything() actually does might help. Most functions return something that can be treated as either a TRUE or a FALSE value. In the code -


somefunction() or anything();


When somefunction() returns a TRUE value, the anything() after the logical or is not evaluated due to short-circuit optimization (TRUE or xxxxx is always TRUE so the evaluation stops at that point.)


When somefunction() returns a FALSE value, the or anything() causes the anything() statement to be evaluated/executed.


The above is a simple conditional test. However, with an actual if(){}else{} conditional test you have the ability to both specify code that is to be execuited when the condition is true and when it is false.

Link to comment
Share on other sites

This thread is more than a year old. Are you sure you have something important to add to it?

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.