How can I update table2 in the below code only if the updation in table1 is success?
$sql="update table1 set col1='abc' where col2='1'";
$result=mysql_query($sql);
if(that is success)
{
$sql="update table2 set col1='cde' where col2='1'";
$result=mysql_query($sql);
}
See the mysql_affected_rows()
function, which will return the number of rows just updated. Also, try to avoid using mysql_* functions in favor of mysqli_* or PDO, as they are now deprecated.
First off, it is highly recommended to not use mysql_*
functions as they are deprecated as of PHP v5.5.0 .
You can check directly with the $result
variable
$result = mysql_query($sql);
if (!$result) {
die('Update failed: ' . mysql_error());
} else {
$sql="update table2 set col1='cde' where col2='1'";
$result=mysql_query($sql);
}
if the above query is succeeded so the $result
variable will have the number 1 as $result else it will have another value so simply you can test it like this
if($result == 1)
{
//Your second update result
}
Try this and tell me the result :)
手册指出,如果更新有效,则更新的返回值为true,否则为false,因此您可以继续检查$return
的值以查看更新是否有效。
Do not use only $result as it will always be true as long as the query didnt cause an error. If the Where clause has no hits, nothing is updated and no error is caused. So result would still be true.
Use mysql_affected_rows as Alex suggested to know how many record have been updated.
So do if ($result && mysql_affected_rows() )
...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.