Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource

Discussion in 'PHP' started by siyah miyah, Apr 30, 2008.

  1. #1
    <?php
    // Include the database connection file.
    include("connection.php");

    // username and password sent from form
    $username=$_POST['myusername'];
    $password=$_POST['mypassword'];

    // To protect MySQL injection (more detail about MySQL injection)
    $username = stripslashes($username);
    $password = stripslashes($password);
    $username = mysql_real_escape_string($username);
    $password = mysql_real_escape_string($password);

    $sql=$query = "select * from users where f_name = ".$username ." AND p_wrd = ".$password." ";
    $result=mysql_query($sql);

    //Count results
    $rowNo = mysql_num_rows($result);
    if ($rowNo =1){

    // Register $myusername, $mypassword
    session_register("myusername");
    session_register("mypassword");
    }
    else {
    echo "Wrong Username or Password";
    }
    ?>

    This was working before, I am not sure why it is not working anymore?
     
    siyah miyah, Apr 30, 2008 IP
  2. graham23s

    graham23s Well-Known Member

    Messages:
    188
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    105
    #2
    heres your problem:

    should be

    you were asigning it the value 1 , rather than equating it to.

    Graham
     
    graham23s, Apr 30, 2008 IP
  3. Randombase

    Randombase Peon

    Messages:
    224
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Also, I'd recommend to add a @ sign in front of mysql_num_rows to avoid errors in case the table is empty or returned zero results:
    $rowNo = @mysql_num_rows($result);
    PHP:
     
    Randombase, Apr 30, 2008 IP
  4. graham23s

    graham23s Well-Known Member

    Messages:
    188
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    105
    #4
    also you may want to trim the white space from the passwords:

    Just a tip.

    also this is wrong:

    just:

    would dso the jobby :)

    Graham
     
    graham23s, Apr 30, 2008 IP
  5. rnilucero

    rnilucero Peon

    Messages:
    45
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    also echo/print or sql statement this help a lot :)
     
    rnilucero, May 1, 2008 IP
  6. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #6
    Replace
    $result=mysql_query($sql);
    Code (markup):
    With
    $result=mysql_query($sql);
    if($result === false)
    {
       die(mysql_error());
    }
    Code (markup):
    Then try it and it should display an error
    What is that error ?
     
    joebert, May 3, 2008 IP
  7. Randombase

    Randombase Peon

    Messages:
    224
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Why not just
    $result = mysql_query($sql) or die(mysql_error());
    PHP:
    :)
     
    Randombase, May 3, 2008 IP