SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Zealot
    Join Date
    May 2007
    Posts
    163
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Issue with mysql_query UPDATE

    I've encountered this and similar situations
    where mysql_query returns a positive response,
    even when the query was not actually executed ;

    Code:
    $upd=mysql_query("UPDATE members set thepassword='$newpass' where actkey='$some_key'");
    
    if($upd)
    
    echo "Your password has been reset.";
    
    else
    
    echo "There is a problem reseting your password:".mysql_error();
    Even when the value of actkey($some_key) does not exist in the
    table, i still get "Your password has been reset."

    What am i doing wrong?

    Thanks

  2. #2
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Put simply, your code is wrong.

    Can you post the code that surrounds the snippet above? You may want to look at mysql_affected_rows too, it returns the number of affected rows by the last INSERT, UPDATE, REPLACE or DELETE query.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  3. #3
    SitePoint Wizard bronze trophy Immerse's Avatar
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    1,661
    Mentioned
    7 Post(s)
    Tagged
    1 Thread(s)
    In this case mysql_query simply return a TRUE to say "yeah, alright, I've gone and updated any matching records".

    You want to check the number of rows that have been affected using mysql_affected_rows().

    PHP Code:
    $upd=mysql_query("UPDATE members set thepassword='$newpass' where actkey='$some_key'"$link);

    $updated mysql_affected_rows($link);

    if(
    $updated>0) {
        echo 
    "Your password has been reset.";
    } else {
        echo 
    "Your password hasn't been reset because you don't exist.";


    Edit>>
    Yay! Anthony beat me to it
    Last edited by Immerse; Jul 25, 2011 at 10:14. Reason: Anthony's soooooo fast!

  4. #4
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Off Topic:

    Quote Originally Posted by Immerse View Post
    Anthony's soooooo fast!
    That's what she said.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  5. #5
    From space with love silver trophy
    SpacePhoenix's Avatar
    Join Date
    May 2007
    Location
    Poole, UK
    Posts
    4,998
    Mentioned
    100 Post(s)
    Tagged
    0 Thread(s)
    If the update query runs and no rows were updated it will still return true.

    PHP Code:
    <?php

    $upd
    =mysql_query("UPDATE members set thepassword='$newpass' where actkey='$some_key'");

    $rows_affected=mysql_affected_rows();

    if (!
    upd) {
        echo 
    "There is a problem reseting your password:".mysql_error();
        
        
    // For a live site you would want to log the mysql error rather then display it on screen
    } elseif ( $rows_affected <> ) {
        echo 
    'Error: Account not found or recognised, please contact the Adminiatrators';
        
    // make suitable log entry
    } else {
        echo 
    "Your password has been reset.";
    }
    ?>
    Community Team Advisor
    Forum Guidelines: Posting FAQ Signatures FAQ Self Promotion FAQ
    Help the Mods: What's Fluff? Report Fluff/Spam to a Moderator

  6. #6
    SitePoint Wizard bronze trophy Immerse's Avatar
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    1,661
    Mentioned
    7 Post(s)
    Tagged
    1 Thread(s)
    Off Topic:

    You win the Michael Scott prize for awesomeness today


    Quote Originally Posted by AnthonySterling View Post
    Off Topic:

    That's what she said.

  7. #7
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,216
    Mentioned
    58 Post(s)
    Tagged
    3 Thread(s)
    Off Topic:

    Quote Originally Posted by AnthonySterling View Post
    Off Topic:

    That's what she said.
    best. line. evar.
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"


Bookmarks

Posting Permissions

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