1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

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:

    PHP:
    1.  
    2. // Define $myusername and $mypassword
    3. $blog_user_name=$_POST['blog_user_name'];
    4. $blog_user_password=$_POST['blog_user_password'];
    5.  
    6. // To protect MySQL injection (more detail about MySQL injection)
    7. $blog_user_name = stripslashes($blog_user_name);
    8. $blog_user_password = stripslashes($blog_user_password);
    9. $blog_user_name = mysql_real_escape_string($blog_user_name);
    10. $blog_user_password = mysql_real_escape_string($blog_user_password);
    11.  
    12. $sql="SELECT * FROM $tbl_name WHERE username='$blog_user_name' and password='$blog_user_password'";
    13. $result=mysql_query($sql);
    14.  
    15. // Mysql_num_row is counting table row
    16. $count=mysql_num_rows($result);
    17. // If result matched $myusername and $mypassword, table row must be 1 row
    18.  
    19. if($count==1){
    20. // Register $myusername, $mypassword and redirect to file "index.php"
    21. session_register("blog_user_name");
    22. session_register("blog_user_password");
    23. header("location:index.php");
    24. }
    25. else {
    26. echo "Wrong Username or Password";
    27. }
    28.  
    29.  
    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 Active Member

    Messages:
    238
    Likes Received:
    8
    Best Answers:
    1
    Trophy Points:
    88
    #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 Active Member

    Messages:
    163
    Likes Received:
    2
    Best Answers:
    1
    Trophy Points:
    83
    #6
    try
    Code (Text):
    1. $result=mysql_query($sql) or die(mysql_error());
    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 Active Member

    Messages:
    163
    Likes Received:
    2
    Best Answers:
    1
    Trophy Points:
    83
    #8
    PHP:
    1. $sql="SELECT * FROM $tbl_name WHERE username='$blog_user_name' and password='$blog_user_password'";
    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 Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #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 Active Member

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

    mim24 Member

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