SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Guru coiL's Avatar
    Join Date
    Sep 2001
    Location
    QLD, Australia
    Posts
    666
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question is this right? (check code please)

    Hi,
    As far as I can tell my script works fine but I wanted to check that I had written it in the right way. Thanks.

    PHP Code:
    $sql "SELECT email FROM user WHERE username = '$username'";
            
                if (!
    mysql_query($sql))
            
    error("A database error occurred in processing your ".
                  
    "request.\\nIf this error persists, please ".
                  
    "contact [email]name@email.com[/email].");
            
            
    $query mysql_query($sql$db);

            
    // Check the database to see if anything matches the entered details.
            
    $number_of_matches = @mysql_num_rows($query);
                
                
    // If there is a matching entry, reset and send the password
                
    if($number_of_matches >= '1')
                    {
        
                
    $result mysql_fetch_object($query);        
                   
    $mail_to $result->email
    coiL
    "cradled in the learning curve"

  2. #2
    SitePoint Wizard Mincer's Avatar
    Join Date
    Mar 2001
    Location
    London | UK
    Posts
    1,140
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: is this right? (check code please)

    IMHO your code is flawed. You perform the query twice when there is no need to, which is a waste of resources. And as part of your logic, even if the query fails, you still try and do it again.

    Also when you check if number_of_matches is greater than or equal to one, you should not put the 1 in quotes as it is a numerical value you are checking, not a string. And, why are you checking greater than or equal to 1 and then only processing one result? You should either be checking that you have exactly one match and then processing it (or failing), or checking for more than that, and looping through the results (the code below is edited to assume you want one match).

    PHP Code:
    $sql "SELECT email FROM user WHERE username = '$username'" ;

    $query = @mysql_query$sql ) ;

    if( ! 
    $query )
    {
        
    error("A database error occurred in processing your ".
              
    "request.\nIf this error persists, please ".
              
    "contact [email]name@email.com[/email].") ;
    }
    else
    {
        
    // If there is a matching entry, reset and send the password
        
    if( mysql_num_rows$query ) == )
        {
            
    $result mysql_fetch_object$query ) ;
            
    $mail_to $result->email ;
        }
        else
        {
            
    error"There was no distinct match" ) ;
        }

    Hope this helps,

    Matt.


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
  •