Use profile script isn't working

Post here is you are having problems with any of the tutorials.
Post Reply
peterr
Posts: 12
Joined: Wed Oct 12, 2011 8:00 pm

Use profile script isn't working

Post by peterr »

Hi guys,

I just finshed the two parts, and the video shows that an array is getting displayed once the scripts of both parts are created, but instead of seeing an array, I see error.

These are the errors I get when I open the user_list.php:

Warning: include(D:\Program Files\xampp\htdocs\userprofile\core/inc/user.inc.php) [function.include]: failed to open stream: No such file or directory in D:\Program Files\xampp\htdocs\userprofile\core\init.inc.php on line 10

Warning: include() [function.include]: Failed opening 'D:\Program Files\xampp\htdocs\userprofile\core/inc/user.inc.php' for inclusion (include_path='.;D:\Program Files\xampp\php\PEAR') in D:\Program Files\xampp\htdocs\userprofile\core\init.inc.php on line 10

Fatal error: Call to undefined function fetch_users() in D:\Program Files\xampp\htdocs\userprofile\user_list.php on line 17

I've put all the files in the right folders (init.inc.php is also in the core folder in the userprofile tutorial folder). What's going wrong?

This is the code I have so far:

init.inc.php
<?php

session_start();

mysql_connect('localhost', 'root', 'erohak');
mysql_select_db('user_profile');

$path = dirname(__FILE__);

include("{$path}/inc/user.inc.php");

$_SESSION['iud'] = 1;

?>
user.inc.php
<?php

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

?>
user_list.php
<?php

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

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta http-equiv="content-type" content="text/html; charset=UTF-8">
		<title>registered user</title>
	</head>
	<body>
		<div>
			<?php
			
			print_r(fetch_users());
			
			?>
		</div>
	</body>
</html>
Who can help me out?
icey2k
Posts: 16
Joined: Mon Jul 04, 2011 4:26 pm

Re: Use profile script isn't working

Post by icey2k »

peterr wrote:
$result = mysql_query('SELECT 'id' AS 'id', 'username' AS 'username' FROM 'users'');
It should be
SELECT `id`, `username` FROM `users`

You've switched on ` and '
User avatar
EcazS
Posts: 789
Joined: Fri May 06, 2011 5:29 pm

Re: Use profile script isn't working

Post by EcazS »

The first two errors are because you're specifying the wrong paths to the files you wanna include, so check your paths and quadruple-check your code. The third error would be a side effect of you having the wrong paths. So if you fix the path issues the third issue should be resolved.
icey2k wrote: It should be
SELECT `id`, `username` FROM `users`
You've switched on ` and '
Fairly certain it doesn't matter in this case. It is recommended to use ` though.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Use profile script isn't working

Post by jacek »

If the paths are definitely correct, next try changing the / to \ since you may not be able to mix them in paths like that. I know you can use either, not sure if you can combine them. Just guessing.
Image
peterr
Posts: 12
Joined: Wed Oct 12, 2011 8:00 pm

Re: Use profile script isn't working

Post by peterr »

I made it work, thanks. I had made a mistake with the paths.

The next question is:

I get the following error:
Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in D:\Program Files\xampp\htdocs\userprofile\core\inc\user.inc.php on line 27
Below this error i can see the fields username, email and id (I've requested different data as I'm using a different database for a different website). What's wrong with the mysql_fetch_assoc?

The code:
<?php

//fetches all of the users from the table
function fetch_users(){
	$result = mysql_query('SELECT `id` AS `id`, `username` AS `username` FROM `users`');

	$users = array();
	
	while (($row = mysql_fetch_assoc($result)) !==false){
		$users[] = $row;
	}
	
	return $users;
}

//fetches profile information for the given user
function fetch_user_info($uid){
	$uid = (int)$uid;
	
	$sql = "SELECT
				`username` AS `username`,
				`email` AS `email`,
				FROM `users`
				WHERE `id` = {$uid}";
	$result = mysql_query($sql);
	
	return mysql_fetch_assoc($result);
}

?>
And the profile.php:
<?php

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

$user_info = fetch_user_info($_GET['uid']);

print_r($user_info);

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta http-equiv="content-type" content="text/html; charset=UTF-8">
		<title>'s Profile</title>
	</head>
	<body>
		<div>
			<h1></h1>
			<p>Username:</p>
			<p>Email:</p>
			<p>Id:</p>
			<p></p>
		</div>
	</body>
</html>
What's wrong?
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Use profile script isn't working

Post by jacek »

`email` AS `email`,
You should not have a , at the end of this line.

for future reference, if you add
echo mycel_error();
after which ever query is causing the problem, it will tell you what the problem is.

Once other thing, your use of the AS keyword is a little odd, there is no point in setting the columns alias to it's actual name. :?
Image
peterr
Posts: 12
Joined: Wed Oct 12, 2011 8:00 pm

Re: Use profile script isn't working

Post by peterr »

Thanks mate.

I copy everything you do in the tutorial. In the tutorial videos you're using AS, so that's the reason I'm using it too. After all, you're the teacher and I have to listen to you :mrgreen:
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Use profile script isn't working

Post by jacek »

Did I not use different names ?
`something` AS `something_else`
:? ?
Image
peterr
Posts: 12
Joined: Wed Oct 12, 2011 8:00 pm

Re: Use profile script isn't working

Post by peterr »

I'm confused!!!

One last question, how can I let the uid variable check the username instead of the user id. So, the url is going to be mysite.com/profile.php?uid=peter?

Thanks in advance!

Peter
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: Use profile script isn't working

Post by Temor »

jacek wrote:
`email` AS `email`,
You should not have a , at the end of this line.

for future reference, if you add
echo mycel_error();
after which ever query is causing the problem, it will tell you what the problem is.

Once other thing, your use of the AS keyword is a little odd, there is no point in setting the columns alias to it's actual name. :?
I just have to point out a typo.
echo mycel_error();
wont do much. It should be
echo mysql_error();
peterr wrote:I'm confused!!!

One last question, how can I let the uid variable check the username instead of the user id. So, the url is going to be mysite.com/profile.php?uid=peter?

Thanks in advance!

Peter
I'd do it like this... There is probably a better, more efficient way but this should suffice.

I'd start by changing $uid to $username in fetch_user_info.
function fetch_user_info($username){
        $username = mysql_real_escape_string($username);
       
        $sql = "SELECT
                                `email` AS `email`
                                FROM `users`
                                WHERE `username` = {$username}";
        $result = mysql_query($sql);
       
        return mysql_fetch_assoc($result);
}
I would then change this line in profile.php.
$user_info = fetch_user_info($_GET['username']);
This should make this url: mysite.com/profile.php?username=peter show peter's profile.
Post Reply