Warning: mysql_num_rows() expects parameter 1 to be resource

Discussion in 'PHP' started by sudip03, May 1, 2010.

  1. #1
    I have a form validation page. database connection is checked. all ok. the post vasiable are also checked but still this simple program is throwing an error.


    $name=$_POST['username'];
    $pass=md5($_POST['password']);

    $sql="select * from admi n_user where
    admin_name= '".$name."' and
    admijn_password= '".$pass."'";

    $rs=mysql_query($sql);
    $count=mysql_num_rows($rs);
    echo $count;


    It's just a simple program i have done this thousand time but i don't now why this is throwing a warning. Recently i have upgraded my wamp version
    PHP 5.3.0
    Mysql 5.1.36

    can any one suggest me the possible reason
     
    sudip03, May 1, 2010 IP
  2. m-1o

    m-1o Guest

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    do you connect with server ?
    In any line there is wrong ?
     
    m-1o, May 1, 2010 IP
  3. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #3
    Ensure that you escape user submitted data before querying, to avoid sql parsing issues, furthermore ensure the table 'admin_user' exists and the column's 'admin_name' and 'admin_password' exist.

    <?php
      $name = mysql_real_escape_string($_POST['username']);
      $pass = md5($_POST['password']);
      
      //change the following query to the correct table/columns if need be...
      $sql = "SELECT * FROM admin_user WHERE admin_name = '{$name}' AND admin_password = '{$pass}'";
      
      $rs = mysql_query($sql);
      $count = mysql_num_rows($rs);
      echo $count;
    ?>
    PHP:
     
    danx10, May 1, 2010 IP
  4. Rory M

    Rory M Peon

    Messages:
    1,020
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    0
    #4
    $name=$_POST['username'];
    $pass=md5($_POST['password']);
    
    $sql="SELECT * FROM `admin_user` WHERE
    `admin_name`= '".$name."' AND 
    `admijn_password`= '".$pass."'";
    
    $rs=mysql_query($sql);
    if(!$rs)
    {
    die(mysql_error());
    }
    $count=mysql_num_rows($rs);
    echo $count;
    
    PHP:
    Try the above, even if it doesn't fix it it, it should give more information for you to work with. Furthermore, is your mysql_connect and mysql_select_db in order?
     
    Rory M, May 1, 2010 IP
  5. dapper

    dapper Active Member

    Messages:
    173
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #5
    If you search for any portion of that error message you will find that it means that your query failed to execute due to an error.

    For debugging purposes only (remove it after you are done), echo mysql_error(); on the next line right after the line with the mysql_query() statement.
     
    dapper, Jan 31, 2011 IP
  6. G3n3s!s

    G3n3s!s Active Member

    Messages:
    325
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    80
    #6
    you just bumped 100 days old thread ...
     
    G3n3s!s, Jan 31, 2011 IP