Jump to content

Get affected rows after Delete (mysql)


fivestringsurf

Recommended Posts

I can't seem to get reliable results after deleting a row(s) using mysqli.  The afftect_rows is always 0.  

I'm using php5.2 and the mysqli method

Something like this:

$sql = "
				DELETE FROM classRoster WHERE id=123
			";
			if ($result = $this->mysqli->query($sql)) {
				return $this->mysqli->affected_rows;   ///ALWAYS RETURNS 0
			}else{
				//log error
			}		

I have confirmed that the row is in fact deleted, but still get 0 as affected_rows.   Please help!

Link to comment
https://forums.phpfreaks.com/topic/284557-get-affected-rows-after-delete-mysql/
Share on other sites

i have seen (past) cases with mysql/mysqli functions where using them in a return statement returns null (which would be treated as a zero.) you can try to assign the value to a variable, then return that variable.

 

also, it's possible that your page is being requested twice by your browser and/or a redirect and you are seeing the result of the second page request, which would be a zero since the row would have been deleted on the first page request.

 

you could run a select query before trying to delete it to insure it exists. this would at least get your code to tell you if it has previously been deleted.

Curiously the problem solved itself after a reboot.  I'm running xammp on windows 7 on a laptop and I get alot of weird inconsistencies like this when I change table structures and then run queries with php.  I find I have to restart MySQL after adding table columns or they don't get recognized from php but do with phpMyAdmin. It's strange as I never encounter these problems when using MySQL/php on my mac or production linux server.  I'll just assume it's some low-level installation glitch within xampp that I'll probably never get to the bottom of.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.