虽然如果语句正确,循环不运行

时间:2013-08-29 22:31:44

标签: php mysql

所以我有一个while循环,它确实在数据库和if语句等方面工作..但由于某种原因if语句没有正确运行

这是代码(已编辑)

<?php session_start( ); ?>
<?php include 'core/init.php';
include 'includes/header.php';

require 'core/database/db_connection.php';

function friend_check($user_id, $friend_id) {
$result = mysql_query("SELECT * FROM fyp_friends WHERE user_id={$user_id} AND         friend_id={$friend_id} AND type=1");
$num_rows = mysql_num_rows($result);

echo $num_rows;
}

?>
<link rel="stylesheet" type="text/css" href="style/members.css">
<?php
/*-------------------------------------------------------------------------------------     --------------------*/
  //Get user information from fyp_users
  $sql = ("SELECT user_id, user_name, first_name, last_name, email, profile FROM     fyp_users");
 $user =mysql_query($sql) or die(mysql_error());
 //$userinfo = mysql_fetch_array($user);
 //echo $userinfo['user_id'];

//Get friend information from fyp_users
//$sql = ("SELECT user_id, friend_id, type FROM fyp_friends");
//$friend =mysql_query($sql) or die(mysql_error());
//$friendinfo = mysql_fetch_array($friend);
//echo $friendinfo['user_id'];
?>

<?php
while ($usermain = mysql_fetch_array($user)) {

echo '<div id="friends">';
echo '<div class="profilepiclarge"><img src="', $usermain['profile'], '"alt="',    $usermain['first_name'],'\'s Profile Image"></div>';
?>
<div class="userinfo">
<?php 
echo "</br>{$usermain['first_name']}";
echo "</br>{$usermain['last_name']}";
echo "</br>{$usermain['email']}";
echo "</br>{$usermain['user_name']}</br>";
echo '<div class="viewprofile">';
echo "<a href='profile.php?user_name={$usermain['user_name']}'>View Profile</a>";
echo '</div>';

$sql = ("SELECT user_id, friend_id, type FROM fyp_friends WHERE user_id=       {$usermain['user_id']}");
$friend =mysql_query($sql) or die(mysql_error());

$friend_result = friend_check($session_user_id, $usermain['user_id']);
echo $friend_result;
if ($usermain['user_id'] === $session_user_id) {
echo "This is you"; 
} else if (intval($friend_result) == 1) {
echo '<div class="removefriend">';
    echo"Remove Friend";
    echo '</div>';
} else if (intval($friend_result) == 0) {
echo '<div class="addfriend">';
    echo"Add Friend";
    echo '</div>';
}

echo "</div>";
echo "</div>";

//If statment to go here to see if already friends dont show add friends function also if     current user is logged in sont show it. If already friends the option 
//to remove the user as a fried becomes avalible

}

1 个答案:

答案 0 :(得分:0)

您的第二个查询没有WHERE子句,因此我没有看到您如何列出与相关用户相关的朋友,请将查询更改为:

SELECT user_id, friend_id, type FROM fyp_friends WHERE user_id = $usermain['user_id']