February 11, 201412 yr i am trying to work on a log in page and keep getting this error whether the login is correct or wrong Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\private_message_system\core\inc\user.inc.php on line 10 <?php // checks a given username and password combination, returning the users id function validate_credentials($user_name, $user_password) { $user_name = mysql_real_escape_string($user_name); $user_password = sha1($user_password); $result = mysql_query("SELECT 'user_id' FROM 'users' WHERE 'user_name' = '{$user_name}' AND 'user_password' = '{$user_password}' "); if (mysql_num_rows($result) != 1) { //line 10 return false; } return mysql_result($result, 0); } ?>
February 11, 201412 yr The value of the user_id column, is that either 1 or 0? If so then the 'mysql_num_rows($result)' is either true or false (1 or 0). If you use $num = count($result); and then check the value of $num with your if it will resolve this. {I very much doubt if this is the best solution, and you would need to check this method} Another way you could do this is use the MYSQL_CALC_TOTAL_ROWS in the select statement, but it is a long time since I have implemented that and I can't remember the correct syntax. Then you would need a $num = {/the result which I believe is retrieved with a fetch()/}. Sorry this is rather vague, and I am sure some of the pro's here will spot holes in what I am suggesting, but I hope it is helpful.
February 11, 201412 yr the user_id is set to one as i only have one person in the database Then to check my assumption try editing the user_id to 2, as if I am correct then this will be read as the number 2 whereas the 1 is being interpreted as true.
February 11, 201412 yr Author it was to do with my select statement i was using " '' " instead of " `` "
February 12, 201412 yr If there is an error then mysql_query returns false rather than a results set which was the case here. It is best to always check the result of mysql_query and handling the error if the result is false rather than assume that the query executed without problems. Any reason why you're not using mysqli (mysql improved) rather than mysql functions? Also, storing passwords unencrypted in a database is not generally a good idea. I would always use SHA1 (or MD5) to encrypt the passwords before you store them in the database and then compare the SHA1-ed password against the value in the database. It's considered best practice in web application security.
February 12, 201412 yr <?php // checks a given username and password combination, returning the users id function validate_credentials($user_name, $user_password){ $user_name = mysql_real_escape_string($user_name); $user_password = sha1($user_password); $result = mysql_query("SELECT 'user_id' FROM 'users' WHERE 'user_name' = '$user_name' AND 'user_password' = '$user_password' LIMIT 1"); if(mysql_num_rows($result)== 1) { //line 10 return mysql_result($result); $msg = "It's True!"; } else { $msg = "No User!"; } echo $msg; ?> Try the above mate. I'm a bit of a php nab but I've not seen { } in an SQL statement before.
Create an account or sign in to comment