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
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);
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
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.
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?
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!!
$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) ?
$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'";