MySQL update after the mysql_affected_rows to determine whether the success, no insert, the result is a problem

Source: Internet
Author: User
Tags mysql update
MySQL update after the mysql_affected_rows to determine whether the success, no insert, the result of a problem, the number of table rows multiplied, the code is as follows, solve

Data written to database function save_db ($currencyname _en, $currencyname _cn, $buyingrate, $sellingrate, $middlerate) {//update $query _update = ' Update '. TABLENAME. '                       SET '. ' buyingrate= '. $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_affected_rows ()!=1)//update unsuccessful, try inserting {$query _insert = ' insert INTO '. 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:

MySQL update after the mysql_affected_rows to determine whether the success, no insert, the result of a problem, the number of table rows multiplied, the code is as follows, solve

Data written to database function save_db ($currencyname _en, $currencyname _cn, $buyingrate, $sellingrate, $middlerate) {//update $query _update = ' Update '. TABLENAME. '                       SET '. ' buyingrate= '. $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_affected_rows ()!=1)//update unsuccessful, try inserting {$query _insert = ' insert INTO '. 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 = ?, ...

Wouldn't that be a little 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.