Page 1 of 1

Does code run after "mysql_query() or die()" or does it wait

Posted: Thu Oct 15, 2009 11:42 am
by I0printRob
Hi - I'm struggling to tell what's failing so I realised I don't know enough about how php executes mysql queries.
Will everything happen in order, or could the javascript at the bottom render/fire without the mysql being successful?
Additionally - what's the best way to fire/react to a timeout (does mysql_error() provide that?)
thanks

Code: Select all

 
<?php
include "includes/test_login.php";
include "includes/db_connect.php";
 
$action = escapeStr($_POST['action']);
$fsRef = escapeStr($_POST['fsRef']);
$tagIndex = escapeStr($_POST['tagIndex']);
$owner = escapeStr($_POST['owner']);
 
//Compile query based on action
$query = "";
 
//action = add ---------------------------------------------------------------------------- 
if($action=="add"){
 
    $query .= "INSERT INTO `tags` (";
    $i = 0;
 
    foreach ($_POST as $field => $value){
        $field = escapeStr($field);
 
        if($field!="action"){
            if($i++>0)$query .= ",";//only prepend comma after first time
            $query .= "`$field`";
        }
    }
 
    $query .= ") VALUES (";
    $i = 0;
 
    foreach ($_POST as $field => $value){
        $value = escapeStr($value);
 
        if($field!="action"){
            if($i++>0)$query .= ",";//only prepend comma after first time
            $query .= "'$value'";
        }
    }
 
    $query .= ")";
}
 
//action = update ---------------------------------------------------------------------------- 
if($action=="update"){
 
    $query .= "UPDATE `tags` SET ";
    $i = 0;
    
    foreach ($_POST as $field => $value){
        $field = escapeStr($field);
        $value = escapeStr($value);
 
        if($field!="action" && $field!="fsRef" && $field!="tagIndex" && $field!="owner"){
            if($i++>0)$query .= ",";//only prepend comma after first time
            $query .= "`$field`='$value'";
        }
    }
    $query .= " WHERE `tagIndex`='$tagIndex' AND `fsRef`='$fsRef' AND `owner`='$owner'";
}
 
//action = delete ---------------------------------------------------------------------------- 
if($action=="delete"){
    $query .= "DELETE FROM `tags` WHERE `fsRef`='$fsRef' AND `tagIndex`='$tagIndex' AND `owner`='$owner'";
}
 
 
//Run compiled query -------------------------------------------------------------------------
mysql_query($query) or die(mysql_error());
 
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title> Saved </title>
</head>
<body>
 
<script type="text/javascript">
 
    // Run js having completed DB update
    this.parent["<?php echo $fsRef ?>leaveEditMode"]("<?php echo $action ?>");
 
    // Send status message
    var returnData = '{"fsRef":"<?php echo $fsRef ?>","text":"Database updated: <?php echo $action ?> tag"}';
    this.parent["<?php echo $fsRef ?>cbDisplayStatus"](returnData);
 
    document.location = "../iframes/loading.html";
 
</script>
 
 
</body>
</html>
 

Re: Does code run after "mysql_query() or die()" or does it wait

Posted: Thu Oct 15, 2009 1:57 pm
by califdon
mysql_query() (and most other similar PHP functions) returns a boolean False if the operation fails.

If you place an "or" after such a function, whatever you specify will happen only if the preceding function returns False.

The PHP function die() means just that: the script aborts, after echoing anything that you place within the parentheses.

mysql_error() merely contains the text of any error message generated by MySQL (not by PHP or anything else).

Re: Does code run after "mysql_query() or die()" or does it wait

Posted: Fri Oct 16, 2009 3:58 am
by I0printRob
Ok thanks... so it waits?
Does the html output render only if the statements preceeding it succeed?
If the server takes time to respond, does nothing else happen until the query has resolved true or false?
It could, for example, send the request and continue running the rest of the code. Then when the server responds fire the error.

Re: Does code run after "mysql_query() or die()" or does it wait

Posted: Fri Oct 16, 2009 4:29 am
by litarena
I'm trying to think of a scenario where a linear program continues when one of its commands is waiting for a response. I can't think of one off the top of my head. Mostly what happens in such programs is that commands are run in a sequence from the top of the program to the bottom. If the command on line 13 is "connect to the database" and the database takes ages to respond, the command on line 14 won't run until the answer has come back. Even if you background part of the program it will still wait for a response.

Re: Does code run after "mysql_query() or die()" or does it wait

Posted: Fri Oct 16, 2009 5:20 am
by I0printRob
Well, Ajax will send off a request and continue rendering the page. I also wasn't sure if the HTML content would be treated in the same way as the php. All possibilities tend to go through your mind when things stop working! Thanks.

Re: Does code run after "mysql_query() or die()" or does it wait

Posted: Fri Oct 16, 2009 11:05 am
by califdon
I0printRob wrote:Ok thanks... so it waits?
Does the html output render only if the statements preceeding it succeed?
If the server takes time to respond, does nothing else happen until the query has resolved true or false?
It could, for example, send the request and continue running the rest of the code. Then when the server responds fire the error.
As litarena said, nothing else happens. If it were otherwise, none of the branching logic would make any sense. It's the same with Ajax. The PHP action is to send data to the server, then it continues with the next step. The fact that the server may send some data back later is a separate activity. That's why it's called "asynchronous." But as far as PHP is concerned it did it's action by sending the data. If the sending action failed, for some reason, the script would stop. Yes, everything stops when a die() operation is executed. That's why it's called "die." :wink: You use this syntax when you want the execution to halt because it would make no sense to continue. If you want to proceed with the rest of the PHP and HTML, you would not use die() syntax.