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

Post here is you are having problems with any of the tutorials.
Locked
twiggy
Posts: 58
Joined: Sat Jun 11, 2011 11:11 pm

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

Post 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.
Last edited by twiggy on Tue Jun 14, 2011 5:29 pm, edited 1 time in total.
Tino
Posts: 360
Joined: Thu May 05, 2011 8:55 pm
Location: The Netherlands

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

Post 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.
Please check out my CodeCanyon items.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

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

Post 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.
Image
twiggy
Posts: 58
Joined: Sat Jun 11, 2011 11:11 pm

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

Post 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.
User avatar
kalipsso
Posts: 30
Joined: Thu Jun 09, 2011 3:03 pm
Location: Bucharest
Contact:

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

Post 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...
twiggy
Posts: 58
Joined: Sat Jun 11, 2011 11:11 pm

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

Post 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
twiggy
Posts: 58
Joined: Sat Jun 11, 2011 11:11 pm

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

Post by twiggy »

oops sorry Tino I miss read your post lol I will change them over now
twiggy
Posts: 58
Joined: Sat Jun 11, 2011 11:11 pm

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

Post 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
twiggy
Posts: 58
Joined: Sat Jun 11, 2011 11:11 pm

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

Post 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?
twiggy
Posts: 58
Joined: Sat Jun 11, 2011 11:11 pm

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

Post 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
twiggy
Posts: 58
Joined: Sat Jun 11, 2011 11:11 pm

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

Post 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.
twiggy
Posts: 58
Joined: Sat Jun 11, 2011 11:11 pm

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

Post 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!
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

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

Post by jacek »

twiggy wrote:all is good now!
good news :)
Image
Locked