Page 1 of 1

PHP Login: expects parameter 1 to be resource, boolean given

Posted: Mon Jun 13, 2011 11:16 pm
by twiggy
I started a new thread encase other people are having the same problem, makes it easier to find in a search ;)

Im getting this error:
Warning: mysql_result() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\user_system\core\inc\user.inc.php on line 7
Code:

user.inc.php
<?php

// Checks if the given username exists in the database.
function user_exists($user){
	$user = mysql_real_escape_string($user);
	$total = mysql_query("SELECT COUNT ('user_id') FROM 'users' WHERE 'user_name' = '{$user}' ");
	return (mysql_result($total, 0) == '1') ? true : false;
}

// Checks is the given username and password combination is valid. 
function valid_credentials($user, $pass){
	$user = mysql_real_escape_string($user);
	$pass = sha1($pass);
	$total = mysql_query("SELECT COUNT ('user_id') FROM 'users' WHERE 'user_name' = '{$user}'  AND 'user_password' = '{$pass}' ");
	return (mysql_result($total, 0) == '1') ? true : false;
}

// Adds a user to the database.
function add_user($user, $pass){
	$user = mysql_real_escape_string(htmlentities($user));
	$pass = sha1($pass);
	mysql_query("INSERT INTO 'users' ('user_name', 'user_password') VALUES ('{$user}', '{$pass}') ");
}

?>
register.php:
<?php

include('core/init.inc.php');

if (isset($_POST['username'], $_POST['password'], $_POST['repeat_password']))
{
	if (empty($_POST['username']))
	{
		$errors[] = 'The username cannot be empty.';
	}
	if (empty($_POST['password']) || empty($_POST['repeat_password']))
	{
		$errors[] = 'The password cannot be empty.';
	}
	if ($_POST['password'] !== $_POST['repeat_password'])
	{
		$errors[] = 'Password verfication failed.';
	}
	if (user_exists($_POST['username']))
	{
		$errors[] = 'The username you enetered is already taken.';
	}
	if (empty($errors) == false)
	{
		add_user($_POST['username'], $_POST['password']);
		$_SESSION['username'] = htmlentities($_POST['username']);
		header('Location: protected.php');
		die();
	}
}

?>
Any ideas? I tried a google, but my level of understanding is not great enough yet to work out what's going on.

Re: PHP Login: expects parameter 1 to be resource, boolean g

Posted: Mon Jun 13, 2011 11:53 pm
by Tino
This applies for every query you have.

Around table names and field names, you use backticks ( ` ) rather than quotes ( ' ).

So you need to switch that around.

Re: PHP Login: expects parameter 1 to be resource, boolean g

Posted: Tue Jun 14, 2011 12:34 am
by jacek
Tino wrote:Around table names and field names, you use backticks ( ` ) rather than quotes ( ' ).
That is the cause of the error ^^ :)

And just to add a tip, when ever you get this kind of error it's a good idea to try adding
echo mysql_error();
after the query giving the error as this will get you a bit more info on the problem.

Re: PHP Login: expects parameter 1 to be resource, boolean g

Posted: Tue Jun 14, 2011 5:29 pm
by twiggy
Hi there, this doesn't seem to be the case I have used ' on all my table and field names, it actually took me a while to find the ` on my keyboard lol :)

So its not that, I have added echo mysql_error(); to the end of each mysql statement but it doesn't show anything different ? Still:
Warning: mysql_result() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\user_system\core\inc\user.inc.php on line 7
Whenever I try and submit anything on the register page.

Re: PHP Login: expects parameter 1 to be resource, boolean g

Posted: Tue Jun 14, 2011 5:46 pm
by kalipsso
here...try this and tell me if it works:
<?php

//checks if given username exists in the database.
function user_exists($user){
	$user = mysql_real_escape_string($user);
	
	$total = mysql_query("SELECT COUNT(`user_id`) FROM `users` WHERE `user_name` = '{$user}'");
	
	return (mysql_result($total, 0) == '1') ? true : false;
}
Copy and replace in you page...

Re: PHP Login: expects parameter 1 to be resource, boolean g

Posted: Tue Jun 14, 2011 5:58 pm
by twiggy
Hi there, I did that but now get this:
Fatal error: Cannot redeclare user_exists() (previously declared in C:\xampp\htdocs\user_system\core\inc\user.inc.php:4) in C:\xampp\htdocs\user_system\core\inc\user.inc.php on line 27

Re: PHP Login: expects parameter 1 to be resource, boolean g

Posted: Tue Jun 14, 2011 5:58 pm
by twiggy
oops sorry Tino I miss read your post lol I will change them over now

Re: PHP Login: expects parameter 1 to be resource, boolean g

Posted: Tue Jun 14, 2011 5:59 pm
by twiggy
Hi Tino changed it to this:
<?php

// Checks if the given username exists in the database.
function user_exists($user){
	$user = mysql_real_escape_string($user);
	$total = mysql_query("SELECT COUNT (`user_id`) FROM `users` WHERE `user_name` = '{$user}' ");
	return (mysql_result($total, 0) == '1') ? true : false;    echo mysql_error();

}

// Checks is the given username and password combination is valid. 
function valid_credentials($user, $pass){
	$user = mysql_real_escape_string($user);
	$pass = sha1($pass);
	$total = mysql_query("SELECT COUNT (`user_id`) FROM `users` WHERE `user_name`= '{$user}'  AND `user_password` = '{$pass}' ");
	return (mysql_result($total, 0) == '1') ? true : false;    echo mysql_error();

}

// Adds a user to the database.
function add_user($user, $pass){
	$user = mysql_real_escape_string(htmlentities($user));
	$pass = sha1($pass);
	mysql_query("INSERT INTO `users` (`user_name`, `user_password`) VALUES ('{$user}', '{$pass}') ");    echo mysql_error();

}

?>
But still getting this when trying to submit:
Warning: mysql_result() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\user_system\core\inc\user.inc.php on line 7

Re: PHP Login: expects parameter 1 to be resource, boolean g

Posted: Tue Jun 14, 2011 6:04 pm
by twiggy
ok kalipsso I tried your text again, I have no idea why it looks exactly the same as mine word for word (see my copy and paste below) but I no longer get that error message but nothing is sent to the database? The form resets and the page reloads but nothing is showing in the db?

Re: PHP Login: expects parameter 1 to be resource, boolean g

Posted: Tue Jun 14, 2011 6:12 pm
by twiggy
ok I got it!

I put:
$total = mysql_query("SELECT COUNT (`user_id`)
and it should be:
$total = mysql_query("SELECT COUNT(`user_id`)
Who would have thought a space after COUNT could cause all that :)

Now time to see why its not writing to my DB

Re: PHP Login: expects parameter 1 to be resource, boolean g

Posted: Tue Jun 14, 2011 6:47 pm
by twiggy
Out of interest if I have entered my DB details incorrectly would I receive an error? As of now I'm not getting anything just the page resets.

Re: PHP Login: expects parameter 1 to be resource, boolean g

Posted: Tue Jun 14, 2011 7:35 pm
by twiggy
Sorry guys i was very tired last night lol :D I added some code to my register page that I have no idea why :shock: that was stopping it, all is good now!

Re: PHP Login: expects parameter 1 to be resource, boolean g

Posted: Wed Jun 15, 2011 1:19 am
by jacek
twiggy wrote:all is good now!
good news :)