New users want to teach mysql to use update to modify data that does not exist.

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.