Page 1 of 1

User Profile "fetch_user_info" function error.

Posted: Thu Aug 09, 2012 11:13 am
by rabatos
I watched the User Profle tutorial videos and created my files.

The 'user.inc.php' has error, as when I navigate to a profile such as http://localhost/members/profile.php?id=7
I get this error:

Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in C:\Websites\members\core\user.inc.php on line 33

I have tried to fix the error, but everything I thought would work didn't and in turn created other errors.

Anyone mind giving me a hand with this?

Here is my user.inc.php file
<?php

//fetches all of the users from the table.
function fetch_users(){
    $result = mysql_query("SELECT id AS id, company_name AS company_name FROM trades");
		
	$users = array();
	
	while (($row = mysql_fetch_assoc($result)) !== false){
	    $users[] = $row;
	}
	
	return $users;
}

//fetches profile info.
function fetch_user_info($id){
    $id = (int)$id;
	
	$sql = "SELECT
				id AS id,
				firstname AS firstname,
				surname AS surname,
				company_name AS company_name,
				email AS email,
				location AS location,
				info AS info
			FROM trades
			WHERE 'id' = {'$id'}";
			
	$result = mysql_query($sql);
	
	return mysql_fetch_assoc($result);
	
}

//updates the current users profile info.
function set_profile_info($email, $location, $info){
	$email = mysql_real_escape_string(htmlentities($email));
	$location = mysql_real_escape_string($location);
	$info = mysql_real_escape_string(nl2br(htmlentities($info)));
	
	$sql = "UPDATE 'trades' SET
				'email' = '{$email}'
				'location' = '{$location}'
				'info' = '{$info}'
			WHERE 'id' = {$_SESSION['id']}";
		
		mysql_query($sql);
}
	
?>
Let me know if you require any other files to look at.

Thanks

Re: User Profile "fetch_user_info" function error.

Posted: Sun Aug 12, 2012 12:44 am
by jacek
id is a mysql keyword, you will need to put backticks around your table and column names.

So instead of
$result = mysql_query("SELECT id AS id, company_name AS company_name FROM trades");
you need
$result = mysql_query("SELECT `id` AS `id`, `company_name` AS `company_name` FROM `trades`");
you only really need than for the id one, but I usually just always use them as it's easier than remembering all of the keywords :P

Re: User Profile "fetch_user_info" function error.

Posted: Thu Aug 23, 2012 9:23 am
by rabatos
I forgot to thank you Jacek for fixing this for me.

Thanks

Re: User Profile "fetch_user_info" function error.

Posted: Fri Aug 24, 2012 12:02 am
by KnightMaire
I would disagree, wide_load.
Every MySQL table of mine uses id as a column, and I have never once used back ticks in my queries. His problem forced the need to use backticks because he is assigning values to itself "someName AS someName" whereas "someName" would produce the exact same result.