PDA

View Full Version : One after another.


hawkal
05-06-2007, 09:51 PM
Hi in php I am getting it to do something then after it has done that it will check if it is done and working properly. The problem is that because the thing it does in the first place has to be executed so when the script loads it shows the error because what it is checking for has happened yet.

How can I make the part where it checks if everything is ok to wait until after the first action has been executed?


<?php

// This is the first action that is checked in the second action//
$check =mysql_query("UPDATE `something` SET email='' WHERE `someone` = '" . $yea . "'")
or die('MySQL Error: ' . mysql_error());}

//This is the second action that checks if the first action was successful //
if ($check){
echo "<center>some text here</center>";
}else{
//This is the line that gets echo'd before the script has
//been executed properly giving people the
//impression there is a problem with the script.
echo "<center>There is something wrong with the
removal script please contact the admin</center>";
}
?>



I hope you understand.

Thanks

PappaJohn
05-06-2007, 10:45 PM
Aside from an extra closing "}" on your first line,

// This is the first action that is checked in the second action//
$check =mysql_query("UPDATE `something` SET email='' WHERE `someone` = '" . $yea . "'")
or die('MySQL Error: ' . mysql_error());}

should be

// This is the first action that is checked in the second action//
$check =mysql_query("UPDATE `something` SET email='' WHERE `someone` = '" . $yea . "'")
or die('MySQL Error: ' . mysql_error());



it looks as if it should be doing what you want, assuming the sql statement is valid. mysql_query can return false for a number of reasons without returning an error.

hawkal
05-06-2007, 10:49 PM
Thanks for the reply the thing is the script works fine, that is with out php errors but when you load the page at the bottom it says "There is something wrong with the removal script please contact the admin" but that is because what it is checking for something in the if statement has not happened yet. How can I delay the if statement until after the first action has been executed?

firepages
05-07-2007, 02:22 AM
well your query can't be working (so $check is false & the error gets displayed), you dont have to `wait` for a query result in these circumstances.

try

<?
if(mysql_affected_rows()){
//update ocurred
} else{
//update failed
}
?>


note that mysql_affected_rows() will be false if nothing was updated (in your example if the email was already blank)

PappaJohn
05-07-2007, 04:37 AM
but that is because what it is checking for something in the if statement has not happened yet.
As firepages said, and I tried to explain earlier, that is not the case. php will complete the mysql_query() returning a value to $check before it moves on the the next instruction.

Just because you are not getting any php errors, and no mysql errors, doesn't mean the sql statement is successful. You need to look at why your sql is failing.

hawkal
05-07-2007, 10:34 AM
Thanks for the help guys!:thumbsup: