Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

mysql_affected_rows problem

Status
Not open for further replies.

ID10T16

Technical User
Sep 22, 2000
160
US
I'm trying to use mysql_affected_rows to determine if an update was done or not.

Here's a query string:

UPDATE staff SET first_name='first', email='', last_name='last', phone='', bio='', sec_lvl='MSTR', active='Y', dept='MSTR' WHERE id='2'

My problem is that if I use if($result), an update may not have been made, but the that condition is considered passing.

If I use mysql_affected_rows, then it says it has failed everytime. The result of affected rows is always 0, but the update may still have been done.

id can equal a number or id can equal nothing, and it will always 'say' it failed in affected_rows, or it will always 'say' it passed if I merely use if($result).

Also, here's the pre version of the query string:

$query_string="UPDATE staff SET first_name='$first_name', email='$email', last_name='$last_name', phone='$phone', bio='$bio', sec_lvl='$sec_lvl', active='$active', dept='$dept' WHERE id='$id' ";

$result=mysql_query("$query_string")or die("<b>A fatal MySQL error occured</b>.\n<br />Query: " . $query_string . "<br />\nError: (" . mysql_errno() . ") " . mysql_error());

Anyone have anything to rectify my stupidity?
 
With an UPDATE query, mysql_query() returns TRUE on success and FALSE on failure. But all those values really mean is whether MySQL accepted the query as well-formed, not that the query actually did anything.


Print the query out.
Copy-and-paste the query into an application that is connected to MySQL and can get MySQL to run an arbitrary query.
Run the query and see how many rows are affected.


It could be the query is running correctly and you are incorrect to think there are any rows that would be updated. It could be that you are in your query quoting some value that should not be or vice-versa and you're running afoul of MySQL's default conversion.





Want the best answers? Ask the best questions! TANSTAAFL!
 
Actually, I am able to immediately see that the change took place immediately after running the query (via a separate window that I refresh that shows the data in the table), but my condition statements failed to work properly is all.

 
I think I figured out the problem. if you do this...

echo mysql_affected_rows();

and then do this right after...

if(mysql_affected_rows() > 0)

Then it doesn't work. I assume this is because when you run mysql_affected_rows, it will thereafter be invalid until another query is performed.

Live and Learn I guess.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top