How do I know if my update statement was successful

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
Antek_Ryu
Forum Commoner
Posts: 34
Joined: Tue Aug 09, 2005 10:55 am

How do I know if my update statement was successful

Post by Antek_Ryu »

Hi All

How do I know if my update statement was successful when updating my database using php. I am working with php5 and mysql


Regards
Antek
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

malcolmboston wrote:mysql_affected_rows
update statement could be successful without any rows being updated, for example:

Code: Select all

update table set column=123 where 2<1
would never update any rows, but it would succeed always (provided that `table` exists and there's `column` column in it)
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

Well for a start, mysql_query() will not return false.. that proves it was a 'successful' statement, and mysql_affected_rows() returns how many rows were updated, this proving the criteria was correct/incorrect.

Using a bit of common sense you can work out if the intended results match the actual results..
Last edited by Jenk on Mon Oct 10, 2005 8:06 am, edited 1 time in total.
User avatar
Nathaniel
Forum Contributor
Posts: 396
Joined: Wed Aug 31, 2005 5:58 pm
Location: Arkansas, USA

Post by Nathaniel »

Code: Select all

mysql_query("UPDATE `table` SET `forks` = 4 WHERE `forks` < 4") or die("An error occurred");
Post Reply