Page 1 of 1

Profile System Errors

Posted: Sat Jun 30, 2012 1:10 pm
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
<?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

   }
?>
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.
<?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>
password and database hidden for security purposes

Re: Profile System Errors

Posted: Sat Jun 30, 2012 1:46 pm
by Thunderbob
I made some changes.
<?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>
<?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);
   }
?>

Re: Profile System Errors

Posted: Sat Jun 30, 2012 3:01 pm
by jacek
First thing, you should only define functions in the .inc.php files so this code
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);
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
echo mysql_error();
under the failing mysql_query() it should tell you what the problem is.

Re: Profile System Errors

Posted: Sat Jun 30, 2012 7:56 pm
by Thunderbob
works! your are awesome!