Go Back   CodingForums.com > :: Server side development > MySQL

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-16-2013, 07:37 AM   PM User | #1
durangod
Senior Coder

 
Join Date: Nov 2010
Posts: 1,177
Thanks: 214
Thanked 31 Times in 30 Posts
durangod is on a distinguished road
result question on id that does not exist

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.

PHP Code:

I decided to test the value of result like this


                
$query "DELETE FROM users WHERE ID='$seldom' LIMIT 1";
                
$result mysqli_query($myconnect,$query);

            if(
$result)
            {
             echo 
"<pre>";
             
print_r($result);
             exit;
             } 

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.

thanks


UPDATE

This does seem to work

PHP Code:

     $jobdone 
mysqli_affected_rows($myconnect);

     if(
$jobdone 0)
     {             
       
//query performed action
       
}else{
                 
// val 0 no action
                 // -1 error 
              //query performed no action
               


Last edited by durangod; 02-16-2013 at 07:52 AM.. Reason: update
durangod is offline   Reply With Quote
Old 02-17-2013, 01:37 AM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,195
Thanks: 59
Thanked 3,995 Times in 3,964 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
I know it's kind of a strange and apparenly out-of-date idea, buy you might try reading the PHP manual:
http://www.php.net/manual/en/function.mysql-query.php

I quote (in part):
Quote:
...
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.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is online now   Reply With Quote
Old 02-17-2013, 02:32 AM   PM User | #3
durangod
Senior Coder

 
Join Date: Nov 2010
Posts: 1,177
Thanks: 214
Thanked 31 Times in 30 Posts
durangod is on a distinguished road
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.

Thanks.
durangod is offline   Reply With Quote
Old 02-17-2013, 04:13 AM   PM User | #4
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,195
Thanks: 59
Thanked 3,995 Times in 3,964 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.

For example,
Code:
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).
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.

Last edited by Old Pedant; 02-17-2013 at 04:16 AM..
Old Pedant is online now   Reply With Quote
Users who have thanked Old Pedant for this post:
durangod (02-17-2013)
Old 02-17-2013, 05:14 AM   PM User | #5
durangod
Senior Coder

 
Join Date: Nov 2010
Posts: 1,177
Thanks: 214
Thanked 31 Times in 30 Posts
durangod is on a distinguished road
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
durangod is offline   Reply With Quote
Old 02-17-2013, 05:25 AM   PM User | #6
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,195
Thanks: 59
Thanked 3,995 Times in 3,964 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
??? 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.)
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is online now   Reply With Quote
Old 02-17-2013, 05:35 AM   PM User | #7
durangod
Senior Coder

 
Join Date: Nov 2010
Posts: 1,177
Thanks: 214
Thanked 31 Times in 30 Posts
durangod is on a distinguished road
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.

Last edited by durangod; 02-17-2013 at 05:42 AM.. Reason: clarify
durangod is offline   Reply With Quote
Old 02-17-2013, 07:07 AM   PM User | #8
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,195
Thanks: 59
Thanked 3,995 Times in 3,964 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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).
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is online now   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:41 PM.


Advertisement
Log in to turn off these ads.