Jump to content

Get affected rows after Delete (mysql)


Go to solution Solved by 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.

Edited by mac_gyver
  • Solution

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.