Profile System Errors

Post here is you are having problems with any of the tutorials.
Post Reply
Thunderbob
Posts: 46
Joined: Sat Jun 30, 2012 12:31 pm

Profile System Errors

Post by Thunderbob »

Hey guys!
I was following along with the Youtube Video and I came across some unique errors that I could not find the answer to here.
So now I am displaying it.
User_list.php
[syntax=php]<?php
session_start();
$con = mysql_connect("/*snip*/","/*snip*/","/*snip*/");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
if (!mysql_select_db("ytrmembership")) {
echo "Unable to select mydbname: " . mysql_error();
exit;
}
mysql_select_db("ytrmembership", $con);



function fetch_users(){
$result = mysql_query("SELECT `id_user` AS `id`, `username` AS `username` FROM `fgusers3` ");

$users = array();

while (($row = mysql_fetch_assoc($result)) !== false){
$users[] = $row;
}
return $users;
}
function fetch_user_info($uid){
$uid = (int)$uid;
$sql = "SELECT
`username` AS `Username`,
`email` AS `email`,
`user_about` AS `About`,
`user_location` AS `location`,
`user_gender` AS `gender`
FROM `fgusers3`
WHERE `user_id`= {$uid}" ;
$result = mysql_query($sql);
return mysql_fetch_assoc($result); <--- error here

}
?>[/syntax]

error: This is what the page looks like

Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in /home/content/66/9481266/html/source/user.inc.php on line 37
Username

Gender:Female

Location:

About:


Anyone can take a stab at this?

Here is the profile page.
[syntax=php]<?php

include('profiledb.php');
$user_info = fetch_user_info($_GET['uid']);



?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content=text/html; charset=utf-8" />
<title><?php echo $user_info['Username']; ?>'s Profile</title>
</head>
<body
<div>
<?php
if (!$sql === NULL){
echo 'That user does not exist.';
} else {
?>
<h1><?php echo $user_info['username']; ?></h1>
<p>Username</p>
<p> Gender:<?php echo ($sql['gender'] == Male) ? 'Male' : 'Female'; ?></p>
<p>Location: </p>
<p>About:</p>
<?php
}
?>
</div>
</body>
</html>[/syntax]

password and database hidden for security purposes
Last edited by jacek on Sat Jun 30, 2012 2:58 pm, edited 1 time in total.
Reason: Removed database password.
Thunderbob
Posts: 46
Joined: Sat Jun 30, 2012 12:31 pm

Re: Profile System Errors

Post by Thunderbob »

I made some changes.

[syntax=php]<?php

include('init.inc.php');
$user_info = fetch_user_info($_GET['uid']);



?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content=text/html; charset=utf-8" />
<title><?php echo $user_info['Username']; ?>'s Profile</title>
</head>
<body
<div>
<?php
if ($user_info === false){
echo 'That user does not exist.';
} else {
?>
<h1><?php echo $user_info['username']; ?></h1>
<p>Username</p>
<p> Gender:<?php echo ($sql['gender'] == Male) ? 'Male' : 'Female'; ?></p>
<p>Location: </p>
<p>About:</p>
<?php
}
?>
</div>
</body>
</html>[/syntax]

[syntax=php]<?php
$con = mysql_connect("/*snip*/","/*snip*/","/*snip*/");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
if (!mysql_select_db("ytrmembership")) {
echo "Unable to select mydbname: " . mysql_error();
exit;
}
function fetch_users(){
$result = mysql_query("SELECT `id_user` AS `id`, `username` AS `username` FROM `fgusers3` ");
$users = array();
while (($row = mysql_fetch_assoc($result)) !== false){
$users[] = $row;
}
return $users;
}
function fetch_user_info($uid){
$uid = (int)$uid;
$sql = "SELECT
`username` AS `username`,
`email` AS `email`,
`user_about` AS `about`,
`user_location` AS `location`,
`user_gender` AS `gender`
FROM `users`
WHERE `id_user`= ($uid)" ;
$result = mysql_query($sql);
return mysql_fetch_assoc($result);
}
?>[/syntax]
Last edited by jacek on Sat Jun 30, 2012 2:59 pm, edited 1 time in total.
Reason: Removed database password.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Profile System Errors

Post by jacek »

First thing, you should only define functions in the .inc.php files so this code

[syntax=php]session_start();
$con = mysql_connect("/*snip*/","/*snip*/","/*snip*/");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
if (!mysql_select_db("ytrmembership")) {
echo "Unable to select mydbname: " . mysql_error();
exit;
}
mysql_select_db("ytrmembership", $con);[/syntax]
should be moved to the init.inc.php file. :)

Second thing

Thunderbob wrote:Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in /home/content/66/9481266/html/source/user.inc.php on line 37

Means that your query is failing, that causes mysql_query() to return false. When you then pass the result to the next function you get an error because it's not expecting false. If you add

[syntax=php]echo mysql_error();[/syntax]
under the failing mysql_query() it should tell you what the problem is.
Image
Thunderbob
Posts: 46
Joined: Sat Jun 30, 2012 12:31 pm

Re: Profile System Errors

Post by Thunderbob »

works! your are awesome!
Post Reply