View Full Version : result question on id that does not exist
02-16-2013, 07:37 AM
Hi, i am working on my error control, and as a test i ran a url that simulated a delete action on a row id that
did not exist to make sure my error message is triggered properly.
I decided to test the value of result like this.
$query = "DELETE FROM users WHERE ID='$seldom' LIMIT 1";
$result = mysqli_query($myconnect,$query);
The value of result is 1 which i assume means true.
Question, how can result be true when the ID i submitted does not exist?
I wonder if mysqli_affected_rows would be a better choice.
This does seem to work
$jobdone = mysqli_affected_rows($myconnect);
if($jobdone > 0)
//query performed action
// val 0 no action
// -1 error
//query performed no action
02-17-2013, 01:37 AM
I know it's kind of a strange and apparenly out-of-date idea, buy you might try reading the PHP manual:
I quote (in part):
For other type of SQL statements, INSERT, UPDATE, DELETE, DROP, etc, mysql_query() returns TRUE on success or FALSE on error.
Use ... mysql_affected_rows() to find out how many rows were affected by a DELETE ... statement.
Note that a DELETE statement will only return FALSE if there is an error, just as that says. It is *NOT* an error if no rows are deleted. Just as it is not an error if an UPDATE doesn't find any records to update. Etc.
02-17-2013, 02:32 AM
Hi yes i did read the manual that is where i got the idea for using the affected rows from in the first place because i have not used it before.
My question came from the fact that i did not realize that on a delete specifically that when an id is not found it does nothing but result true, i thought i would give some notice that the record was not found and i have always coded for that fact using result.
Now i know that some of my statement from the past will always be true, i just did not realize that on a delete it would always be true even if the record did not exist.
I do appreciate your help but i also somewhat resent the implication that i did not refer to the manual first, it was just a misunderstanding.
02-17-2013, 04:13 AM
Sorry! I saw your post before you added the UPDATE. But then it was still sitting on my browser screen and I didn't refresh before answering.
You clearly did read the manual, and I apologize.
You would know that SQL (any kind of SQL, be it MySQL, SQL Server, Oracle, or even Access) does not *EVER* consider it an error (or even a warning) when there are no rows affected by a DELETE or UPDATE (or SELECT! think about it!) if you had experience doing ad hoc queries directly to the database.
mysql> delete from users where userid = 777;
Query OK, 0 rows affected (0.00 sec)
That's from MySQL's command line client. As you can see, it tells me the query is "OK" but then tells me there are zero rows affected.
In a way, I think it's too bad that people don't learn the command line client nowadays. It really gives you the closest interaction with MySQL and the best feeling for how things work, in my opinion. But I do understand the attraction of tools such as phpMySqlAdmin (or whatever the right name is).
02-17-2013, 05:14 AM
Groovy, yeah maybe when i get my own ded server i can play with that but for not it is phpMyAdmin.
Its all good and again thanks :) :thumbsup:
02-17-2013, 05:25 AM
??? You don't run MySQL on your desktop machine? Why not?
Then you can do all your development and debugging locally before uploading to the server.
Man, I could not *LIVE* with having to do work directly on the production server! (Well, I could, and I have, but it's at least 3 times more difficult.)
02-17-2013, 05:35 AM
lol i guess i just never thought about it, i have always done my development on a live server environment in my own test env section, this way i know without a doubt it will work as i stated.
I never (not even in the old days of databus and fortran or JCL ) wanted to have to take my work home with me so i guess over the years i just developed everything on a live environment. I never really believed in many of the localized api or mini dev packages that were available and so i just always do everything on live env.
I guess the one reason i can get away with it is i do alot of work on one particular software (other than my personal stuff) and i also run the same copy in my dev library so its not like im having to upload anything new.
Just as you would have a hard time doing it my way, i would have a hard time doing it your way lol...
Just wanted to clarify though it may be a live env but it is not a public env, doing work on a live site while users are using it is not my cup of tea, although in a crunch i have done it, we all have.
02-17-2013, 07:07 AM
Well, that's some comfort at least (the non-live dev environment).
But I just hate the tedium of upload and test, upload and test.
Of course, I do a lot of work in ASP.NET, and MS's Visual Studio gives you a debug environment that is wonderful: You can hardly tell the difference between debugging server-side and client-side code. I can set breakpoints either place and inspect variable values either place and ... (Well, granted, you are stuck using MSIE as a browser during debug, but that just means I need to make a quick run-through with FF and Chrome after things are debugged, to make sure it all works in them as well.)
Even if you crash, you won't take down your main browser, as VS creates a custom development browser for you (still IE, of course).