mysql_num_rows() expects parameter 1 to be resource, boolean given

Discussion in 'PHP' started by chicago24, Mar 19, 2011.

  1. #1
    Hi everyone,

    This is my first time using a forum so I apologise now if I don't explain myself very well.

    I am very new to php and have followed a tutorial to create a login function however when I type in a username and password that is in the correct table in the database i get the following error:

    Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\wamp\www\Blog2\checklogin.php on line 27

    My code is as follows:

    
    // Define $myusername and $mypassword 
    $blog_user_name=$_POST['blog_user_name']; 
    $blog_user_password=$_POST['blog_user_password']; 
    
    // To protect MySQL injection (more detail about MySQL injection)
    $blog_user_name = stripslashes($blog_user_name);
    $blog_user_password = stripslashes($blog_user_password);
    $blog_user_name = mysql_real_escape_string($blog_user_name);
    $blog_user_password = mysql_real_escape_string($blog_user_password);
    
    $sql="SELECT * FROM $tbl_name WHERE username='$blog_user_name' and password='$blog_user_password'";
    $result=mysql_query($sql);
    
    // Mysql_num_row is counting table row
    $count=mysql_num_rows($result);
    // If result matched $myusername and $mypassword, table row must be 1 row
    
    if($count==1){
    // Register $myusername, $mypassword and redirect to file "index.php"
    session_register("blog_user_name");
    session_register("blog_user_password"); 
    header("location:index.php");
    }
    else {
    echo "Wrong Username or Password";
    }
    
    ob_end_flush();
    
    PHP:
    I really hope someone can help as I have no idea what to do. Thanks in advance :)
     
    chicago24, Mar 19, 2011 IP
  2. AsHinE

    AsHinE Well-Known Member

    Messages:
    240
    Likes Received:
    8
    Best Answers:
    1
    Trophy Points:
    138
    #2
    Usually this error means that you have some errors in query. Try to print your query and run it with phpmyadmin or put echo mysql_error(); right after $result=mysql_query($sql);
     
    AsHinE, Mar 20, 2011 IP
  3. chicago24

    chicago24 Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    It says unknown column username in where clause.

    I don't understand this as I don't have a where clause. Any ideas?

    Thank you for replying :)
     
    chicago24, Mar 20, 2011 IP
  4. crazyryan

    crazyryan Well-Known Member

    Messages:
    3,087
    Likes Received:
    165
    Best Answers:
    0
    Trophy Points:
    175
    #4
    It means your username column doesn't exist, are you sure it's not called blog_user_name, user, etc? If you could post your table structure here we can tell you what column name to use.
     
    crazyryan, Mar 20, 2011 IP
  5. chicago24

    chicago24 Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    The table is simply blog_users and the columns are blog_user_id, blog_user_name and blog_user_password

    Does that help at all?
     
    chicago24, Mar 20, 2011 IP
  6. NLZ13

    NLZ13 Well-Known Member

    Messages:
    166
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    113
    #6
    try
    $result=mysql_query($sql) or die(mysql_error());
    Code (markup):
    What does that say ?
     
    NLZ13, Mar 20, 2011 IP
  7. chicago24

    chicago24 Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    It still says unknown column username in where clause.

    I understand the error message but I don't have a where clause which is confusing me.

    Any ideas?

    Thanks for the responses guys!! :)
     
    chicago24, Mar 20, 2011 IP
  8. NLZ13

    NLZ13 Well-Known Member

    Messages:
    166
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    113
    #8
    $sql="SELECT * FROM $tbl_name WHERE username='$blog_user_name' and password='$blog_user_password'";
    PHP:
    You do have a WHERE clausule. But this definatly means there is no column named "username". So check the exact spelling of your column (CAPS letters, or maybe it is user_name, or a typo like usernam) ?
     
    NLZ13, Mar 20, 2011 IP
  9. stevejohnson

    stevejohnson Member

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #9
    $sql="SELECT * FROM $tbl_name WHERE username='$blog_user_name' and password='$blog_user_password'";

    The bold is the WHERE clause. You said this:
    You're getting the error because there is no column in your table named 'username', nor is there one named 'password'. Use the correct column names:

    $sql="SELECT * FROM $tbl_name WHERE blog_user_name='$blog_user_name' and blog_user_password='$blog_user_password'";
     
    stevejohnson, Mar 20, 2011 IP
  10. chicago24

    chicago24 Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Thank you so much!
    How was I so stupid as to miss the where clause?!
    Thanks again :)
     
    chicago24, Mar 20, 2011 IP
  11. NLZ13

    NLZ13 Well-Known Member

    Messages:
    166
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    113
    #11
    Can happen to anybody ;)
     
    NLZ13, Mar 20, 2011 IP
  12. mim24

    mim24 Well-Known Member

    Messages:
    327
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    103
    #12
    thanks
     
    mim24, Mar 9, 2013 IP