Page 1 of 1

If statement with sql query

Posted: Wed Aug 29, 2007 12:49 pm
by davidtube
I have the following code which i thought would carry out the $update query and if that didn't work (because the row matching the sql WHERE statement can't be found) the $insert query would be run. But the $insert is never carried out. What am i doing wrong?

Code: Select all

if (!mysql_query($updateprice,$con))
  {
mysql_query($insertprice,$con);
  }

Posted: Wed Aug 29, 2007 12:53 pm
by TheMoose
mysql_query will only return FALSE on an error with an update query. Since the query itself is fine, it still runs through and executes, but since the row cannot be found to update, nothing is updated (this is NOT an error). What you should try doing is checking mysql_affected_rows() to see if it's more than 0 (an update occurred), and then continue on to insert if the affected rows is 0.

Posted: Wed Aug 29, 2007 12:59 pm
by davidtube
Perfect, thank you very much :D