Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Senior Coder
    Join Date
    Nov 2010
    Posts
    1,486
    Thanks
    279
    Thanked 32 Times in 31 Posts

    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 08:52 AM. Reason: update

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,664
    Thanks
    80
    Thanked 4,641 Times in 4,603 Posts
    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):
    ...
    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.

  • #3
    Senior Coder
    Join Date
    Nov 2010
    Posts
    1,486
    Thanks
    279
    Thanked 32 Times in 31 Posts
    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.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,664
    Thanks
    80
    Thanked 4,641 Times in 4,603 Posts
    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).
    Last edited by Old Pedant; 02-17-2013 at 05:16 AM.
    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.

  • Users who have thanked Old Pedant for this post:

    durangod (02-17-2013)

  • #5
    Senior Coder
    Join Date
    Nov 2010
    Posts
    1,486
    Thanks
    279
    Thanked 32 Times in 31 Posts
    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

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,664
    Thanks
    80
    Thanked 4,641 Times in 4,603 Posts
    ??? 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.

  • #7
    Senior Coder
    Join Date
    Nov 2010
    Posts
    1,486
    Thanks
    279
    Thanked 32 Times in 31 Posts
    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 06:42 AM. Reason: clarify

  • #8
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,664
    Thanks
    80
    Thanked 4,641 Times in 4,603 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •