SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    a fresh, new start... dujmovicv's Avatar
    Join Date
    Aug 2006
    Location
    Earth
    Posts
    559
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    check user login - mysql error

    Hi All!

    I use this script to check user login within a function :
    PHP Code:
    $query "SELECT * FROM users WHERE username = '$username' AND password = '".md5($password)."'";
            
            
    $result mysql_query($query);
            
            if(
    mysql_num_rows($result) > 0)
                {
                
    //Login Successful
                
    $member mysql_fetch_assoc($result);
                
    $_SESSION['SESS_MEMBER_ID'] = $member['userID'];
                
    //Write session to disc
                
    session_write_close();
                exit();
                }

            else
                {
                
    error('Wrong username or password!');
                } 
    The script seems ok to me, the browser however reports error/warning :
    Code:
    Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in path\to\the\file\index.php on line 213
    Is there a syntax error somewhere I didn't notice?

    Full time ADMIN - art community
    Part time coder - dsign

  2. #2
    SitePoint Member
    Join Date
    Oct 2009
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    my sql anable know that what is mysql_num_rows();,so u check correct the method

  3. #3
    a fresh, new start... dujmovicv's Avatar
    Join Date
    Aug 2006
    Location
    Earth
    Posts
    559
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by onlineadworth View Post
    my sql anable know that what is mysql_num_rows();,so u check correct the method
    Well, that's not the best way to start your SITEPOINT adventure... Hope someone can post a helpful message here.

    Full time ADMIN - art community
    Part time coder - dsign

  4. #4
    SitePoint Member zeng's Avatar
    Join Date
    Oct 2009
    Location
    china tianji
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if(@mysql_num_rows($result) > 0)
    you can use @ aviod warninng
    when username or password is wrong the $result = mysql_query($query);
    will return null so mysql_num_rows() throw a warning

  5. #5
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,510
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    The error indicates that your query has ended with an error.
    Change this line
    Code PHP:
    $result = mysql_query($query);
    into
    Code PHP:
    $result = mysql_query($query) or die(mysql_error());
    and see what error it returns

  6. #6
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,510
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by zeng View Post
    if(@mysql_num_rows($result) > 0)
    you can use @ aviod warninng
    when username or password is wrong the $result = mysql_query($query);
    will return null so mysql_num_rows() throw a warning
    If the query doesn't return any rows, mysql_num_rows returns 0.

  7. #7
    SitePoint Zealot GOPalmer's Avatar
    Join Date
    Jan 2009
    Location
    Wiltshire, UK
    Posts
    125
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    mysql_query() returns false on error, rather than a resource ( required by mysql_num_rows() ). You can do a check like this to ensure the given result is not false, and if it is throw an error.

    PHP Code:
    if (!$result) {
        throw new 
    Exception('Invalid query: ' mysql_error());

    Best Regards, George

  8. #8
    a fresh, new start... dujmovicv's Avatar
    Join Date
    Aug 2006
    Location
    Earth
    Posts
    559
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by guido2004 View Post
    The error indicates that your query has ended with an error.
    Change this line
    Code PHP:
    $result = mysql_query($query);
    into
    Code PHP:
    $result = mysql_query($query) or die(mysql_error());
    and see what error it returns
    Thanks guido, that was the most helpful post! I managed to debug my code.

    Full time ADMIN - art community
    Part time coder - dsign


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
  •