Guys, I need help in this. Please.
index.php
<?php
include ('core/init.inc.php');
?>
<head>
<body>
<?php
foreach (fetch_users() AS $user)
{
?>
<p>
<a href="profile.php?uid=<?php echo $user['id']; ?>"><?php echo $user['username']; ?></a>
<a href="position.php?pid=<?php echo $user['position_id']; ?>"><?php echo $user['position']; ?></a>
</p>
<?php
}
?>
</body>
</head>
core/init.inc.php
<?php
session_start();
mysql_connect('localhost', 'admin', '');
mysql_select_db('users_profile');
$path = dirname(__FILE__);
include("{$path}/inc/user.inc.php");
$_SESSION['uid'] = 1;
?>
core/inc/user.inc.php
<?php
//fetches ALL OF the users FROM the TABLE.
FUNCTION fetch_users(){
$result = mysql_query('SELECT `user_id` AS `id` , `user_username` AS `username`, `position_id` AS posID`, `position_name` AS `position` FROM `users`');
$users = array();
while (($row = mysql_fetch_assoc($result)) !== FALSE){
$users[] = $row;
}
RETURN $users;
}
//fetches profile information FOR the given USER.
FUNCTION fetch_users_info( $uid)
{
$uid = (INT)$uid;
$sql = "SELECT `user_id` AS `id` , `user_username` AS `username`, `position_id` AS posID`, `position_name` AS `position` FROM `users`
WHERE (user_id = {$uid})
";
$result = mysql_query($sql);
RETURN mysql_fetch_assoc($result);
}
FUNCTION fetch_position_info( $pid)
{
$pid = (INT)$uid;
$sql = "SELECT `user_id` AS `id` , `user_username` AS `username`, `position_id` AS posID`, `position_name` AS `position` FROM `users`
WHERE (position_id = {$pid})
";
$result = mysql_query($sql);
RETURN mysql_fetch_assoc($result);
}
?>
profile.php
<?php
include('core/init.inc.php');
$user_info = fetch_user_info($_GET['uid']);
?>
<html>
<head>
<title><?php echo $user_info['username']; ?>'s Profile</title>
</head>
<body>
<?php
if ($user_info === false){
echo 'That USER does NOT exist.' ;
}else{
?>
<h1> <?php echo $user_info['firstname']; ?> <?php echo $user_info['lastname']; ?> </h1>
<p>Username: <?php echo $user_info['username']; ?></p>
<p>Gender: <?php echo ($user_info['gender'] == 1) ? 'Male' : 'Female'; ?></p>
<p>Email:<?php echo $user_info['email']; ?></p>
<p>Locaition: <?php echo $user_info['location']; ?></p>
<p>About me: <?php echo $user_info['about']; ?></p>
<?php
}
?>
</body>
</html>
position.php
<?php
include('core/init.inc.php');
$position_info = fetch_position_info($_GET['pid']);
?>
<html>
<head>
<title><?php echo $position_info['position']; ?>'s Profile</title>
</head>
<body>
<?php
if ($position_info === false){
echo 'That user does not exist.' ;
}else{
?>
<h1> <?php echo $position_info['firstname']; ?> <?php echo $position_info['lastname']; ?> </h1>
<p>Username: <?php echo $position_info['username']; ?></p>
<p>Gender: <?php echo ($position_info['gender'] == 1) ? 'Male' : 'Female'; ?></p>
<p>Email:<?php echo $position_info['email']; ?></p>
<p>Locaition: <?php echo $position_info['location']; ?></p>
<p>About me: <?php echo $position_info['about']; ?></p>
<?php
}
?>
</body>
</html>
Once i run it, profile.php query doesn't run but whenever i remove one function from user.inc.php to this,
<?php
//fetches all of the users from the table.
function fetch_users(){
$result = mysql_query('SELECT `user_id` AS `id` , `user_username` AS `username`, `position_id` AS posID`, `position_name` AS `position` FROM `users`');
$users = array();
while (($row = mysql_fetch_assoc($result)) !== false){
$users[] = $row;
}
return $users;
}
//fetches profile information for the given user.
function fetch_users_info( $uid)
{
$uid = (int)$uid;
$sql = "SELECT `user_id` AS `id` , `user_username` AS `username`, `position_id` AS posID`, `position_name` AS `position` FROM `users`
WHERE (user_id = {$uid})
";
$result = mysql_query($sql);
return mysql_fetch_assoc($result);
}
Then it able to run. Why is that so?