After mysqlUPDATE, use mysql_affected_rows to determine whether the result is successful. If no result is displayed, INSERT will cause a problem.

Source: Internet
Author: User
After mysqlUPDATE, use mysql_affected_rows to determine whether the data is successful. if no data is inserted, the result is incorrect. the number of table rows doubles. the code is as follows: {code ...} after mysql is updated, use mysql_affected_rows to determine whether the operation is successful. If no result is displayed, INSERT. The result is incorrect. the number of table rows doubles. the code is as follows:

// Write data to the database function save_db ($ currencyname_en, $ currencyname_cn, $ buyingrate, $ sellingrate, $ middlerate) {// UPDATE $ query_update = 'update '. TABLENAME. 'set '. 'bucket = '. $ buyingrate. ','. 'sellingrate = '. $ sellingrate. ','. 'middlerate = '. $ middlerate. ','. 'lastupdatetime = "'. date ('Y-m-d H: I: S', time ()). '"'. 'Where currencyname_en = "'. $ currencyname_en. '"'; mysql_query ($ query_update); if (mysql_a Ffected_rows ()! = 1) // update failed. try to INSERT {$ query_insert = 'Insert '. TABLENAME. '(currencyname_en, currencyname_cn, buyingrate, sellingrate, middlerate, lastupdatetime) VALUES ("'. $ currencyname_en. '","'. $ currencyname_cn. '","'. $ buyingrate. '","'. $ sellingrate. '","'. $ middlerate. '","'. date ('Y-m-d H: I: S', time ()). '")'; mysql_query ($ query_insert);} if (mysql_affected_rows ()! = 1) {$ err. ="
Insert or update ". $ currencyname_en."-". $ key." currencyname_cn ";}}

Reply content:

After mysql is updated, use mysql_affected_rows to determine whether the operation is successful. If no result is displayed, INSERT. The result is incorrect. the number of table rows doubles. the code is as follows:

// Write data to the database function save_db ($ currencyname_en, $ currencyname_cn, $ buyingrate, $ sellingrate, $ middlerate) {// UPDATE $ query_update = 'update '. TABLENAME. 'set '. 'bucket = '. $ buyingrate. ','. 'sellingrate = '. $ sellingrate. ','. 'middlerate = '. $ middlerate. ','. 'lastupdatetime = "'. date ('Y-m-d H: I: S', time ()). '"'. 'Where currencyname_en = "'. $ currencyname_en. '"'; mysql_query ($ query_update); if (mysql_a Ffected_rows ()! = 1) // update failed. try to INSERT {$ query_insert = 'Insert '. TABLENAME. '(currencyname_en, currencyname_cn, buyingrate, sellingrate, middlerate, lastupdatetime) VALUES ("'. $ currencyname_en. '","'. $ currencyname_cn. '","'. $ buyingrate. '","'. $ sellingrate. '","'. $ middlerate. '","'. date ('Y-m-d H: I: S', time ()). '")'; mysql_query ($ query_insert);} if (mysql_affected_rows ()! = 1) {$ err. ="
Insert or update ". $ currencyname_en."-". $ key." currencyname_cn ";}}

INSERT INTO table(...)VALUES(...)ON DUPLICATE KEY UPDATE field1 = ?, field2 = ?, ...

Will this be simpler?

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.