Home > Article > Backend Development > PHP+MySQL method to determine whether update statement is executed successfully, mysqlupdate_PHP tutorial
The update statement is a commonly used operation in PHP+MySQL. Determining whether the update statement is executed successfully is a very important step. This article uses an example to demonstrate the method of php+MySQL to determine whether the update statement is executed successfully. Share it with everyone for your reference. The specific method is as follows:
Code 1:
$rs=MySQL_query($sql); if(mysql_affected_rows()) echo "sql执行成功"; else echo "sql执行失败";
Code 2:
<?php /* 连接数据库 */ mysql_pconnect("localhost", "mysql_user", "mysql_passWord") or die ("Could not connect" . mysql_error()); mysql_select_db("mydb"); /* Update 记录 */ mysql_query("UPDATE mytable SET used=1 WHERE id < 10"); printf ("Updated records: %d\n", mysql_affected_rows()); mysql_query("COMMIT"); ?>
To observe the execution of update, mysql_affected_rows() is required.
I believe that what is described in this article has certain reference value for everyone’s PHP+MySQL programming.
if(false!==mysql_query($sql)){ echo 'Success! ';}
This is enough to judge! If something goes wrong, you may have written the wrong code in front of you. Please post it and have a look! You said insert returns false. I guess you have executed mysql_query before and then used it to judge?
.......Connect to the database......
$sql = "update tb_user set name='Sun' where id=1";
$flag = mysql_query ($sql);
if($flag)
{
echo 1;
}
else
{
echo "update error";
}
Reference: Du Niang