Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Warning: mysql_num_rows() expects parameter 1 to be resource

Featured Replies

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);
	}

?>

 

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.

  • Author

the user_id is set to one as i only have one person in the database

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.

  • Author

it was to do with my select statement i was using " '' " instead of " `` "

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.

<?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

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.