[thelist] mysql_affected_rows();

Chris Blessing webguy at mail.rit.edu
Fri Sep 27 08:50:01 CDT 2002


Dunstan-

Actually it seems to be working correctly, except that you have your
comparison value enclosed in single quotes.  Change this:

if ($noar == '0')

to this:

if ($noar == 0)

mysql_affected_rows() returns an integer result, and if you compare it to a
string PHP will implicitly convert the integer result to a string (IIRC)
which is NOT the same as comparing integers to integers.  At that point
you're comparing character values.  Example:

10 does not equal "10"
"10" does equal "10"
"010" does not equal "10"

In fact, "010" equals 145 (0=48, 1=49, 0=48; so 48+49+48).  " 010" (with the
space) equals 177, since the "space" character is character code 32 (so
145+32).  If you try to do arithmatic comparisons to strings, you will start
to get some funny results unless you understand the character code summation
system.

Try changing the above statement to do integer-to-integer comparisons and
see if you get the same results in your error variable.  HTH!

PS: your SQL statement doesn't need to be wrapped with parenthesis.  Use
this instead:

$sql = "UPDATE images SET title = '$title', date_shot = '$date_shot' WHERE
imageid = '$imageid'";

Chris Blessing
webguy at mail.rit.edu
http://www.330i.net

> This is odd.
>
> I'm running an update query on a mysql database.
>
> // images query
> $sql = ("UPDATE images SET title = '$title', date_shot =
> '$date_shot' WHERE
> imageid = '$imageid'");
>
> // run query
> mysql_query($sql);
>
> // count rows affected
> $noar = mysql_affected_rows();
>
> // do errors
> if ($noar == '0')
> 	{
> 	$edit_images_update_error = "1";
> 	}
> else
> 	{
> 	$edit_images_update_error = "0";
> 	}
>
>
>
> If the data that's being updated has altered in any way then my number of
> affected rows ($noar) returns true and my error = 0.
>
> If the data hasn't changed, then $noar == 0, and my error = 1.
>
>
> Does php/mysql compare my data then and decide that as it's the same it
> doesn't need updating?
>
> I don't get it?
>
> Thanks - dunstan




More information about the thelist mailing list