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
[syntax=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;
?>
[/syntax]
user.inc.php
[syntax=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;
}
?>
[/syntax]
user_list.php
[syntax=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>
[/syntax]
Who can help me out?
Use profile script isn't working
Re: Use profile script isn't working
peterr wrote:
[syntax=php]
$result = mysql_query('SELECT 'id' AS 'id', 'username' AS 'username' FROM 'users'');
[/syntax]
It should be
[syntax=sql]SELECT `id`, `username` FROM `users`[/syntax]
You've switched on ` and '
Re: Use profile script isn't working
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.
Fairly certain it doesn't matter in this case. It is recommended to use ` though.
icey2k wrote:It should be
[syntax=sql]SELECT `id`, `username` FROM `users`[/syntax]
You've switched on ` and '
Fairly certain it doesn't matter in this case. It is recommended to use ` though.
Re: Use profile script isn't working
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.
Re: Use profile script isn't working
I made it work, thanks. I had made a mistake with the paths.
The next question is:
I get the following error:
[syntax=text]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[/syntax]
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:
[syntax=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;
}
//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);
}
?>[/syntax]
And the profile.php:
[syntax=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>[/syntax]
What's wrong?
The next question is:
I get the following error:
[syntax=text]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[/syntax]
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:
[syntax=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;
}
//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);
}
?>[/syntax]
And the profile.php:
[syntax=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>[/syntax]
What's wrong?
Re: Use profile script isn't working
[syntax=sql]`email` AS `email`,[/syntax]
You should not have a , at the end of this line.
for future reference, if you add
[syntax=php]echo mycel_error();[/syntax]
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.![Confused :?](./images/smilies/icon_e_confused.gif)
You should not have a , at the end of this line.
for future reference, if you add
[syntax=php]echo mycel_error();[/syntax]
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.
![Confused :?](./images/smilies/icon_e_confused.gif)
Re: Use profile script isn't working
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![Mr. Green :mrgreen:](./images/smilies/icon_mrgreen.gif)
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
![Mr. Green :mrgreen:](./images/smilies/icon_mrgreen.gif)
Re: Use profile script isn't working
Did I not use different names ?
[syntax=sql]`something` AS `something_else`[/syntax]
?
[syntax=sql]`something` AS `something_else`[/syntax]
![Confused :?](./images/smilies/icon_e_confused.gif)
Re: Use profile script isn't working
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
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
Re: Use profile script isn't working
jacek wrote:[syntax=sql]`email` AS `email`,[/syntax]
You should not have a , at the end of this line.
for future reference, if you add
[syntax=php]echo mycel_error();[/syntax]
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.[syntax=php]echo mycel_error();[/syntax] wont do much. It should be[syntax=php]echo mysql_error();[/syntax]
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.
[syntax=php]
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);
}[/syntax]
I would then change this line in profile.php.
[syntax=php]
$user_info = fetch_user_info($_GET['username']);
[/syntax]
This should make this url: mysite.com/profile.php?username=peter show peter's profile.