New users want to teach mysql to use update to modify data that does not exist. It will also be executed successfully. when learning to write code, it will find that if mysql does not use update to modify data, it will also return the execution success. what should I do first use SQL queries to determine whether data exists?
Reply to discussion (solution)
After the update is executed, mysql_affected_rows will return the number of records changed.
You can determine whether the update is executed.
After the update is executed, mysql_affected_rows will return the number of records changed.
You can determine whether the update is executed.
Can you tell me the specific code?
After the update is executed, mysql_affected_rows will return the number of records changed.
You can determine whether the update is executed.
Thank you.
Function ToolDML ($ db, $ SQL) {$ host = "localhost"; $ user = "root"; $ password = "root"; // 1. connect to the database $ conn = mysql_connect ($ host, $ user, $ password); if (! $ Conn) {die (mysql_error ();} // 2. select database mysql_select_db ($ db, $ conn); // 3. set the database character set mysql_query ("set names gbk"); // 4. sending command SQL (ddl data definition statement, dml (data operation language update insert, delete), dql (select), dtl data transaction statement rollback commit ...) $ res = mysql_query ($ SQL, $ conn); if (mysql_affected_rows () = 0) {echo "DML operation not affected";} if (! $ Res) {die (mysql_error ();} // 5. close mysql_close ($ conn) ;}$ SQL = "update price set goodprice = 230 where id = 19;"; $ db = "test"; ToolDml ($ db, $ SQL );
The mysql_affected_rows function can obtain the number of rows affected by the previous MySQL operation.