If statement with sql query

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
davidtube
Forum Commoner
Posts: 79
Joined: Sun Mar 25, 2007 8:42 pm

If statement with sql query

Post 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);
  }
User avatar
TheMoose
Forum Contributor
Posts: 351
Joined: Tue May 23, 2006 10:42 am

Post 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.
davidtube
Forum Commoner
Posts: 79
Joined: Sun Mar 25, 2007 8:42 pm

Post by davidtube »

Perfect, thank you very much :D
Post Reply