我有這3個表:PHP如何查詢從3個表需要的信息在一個select語句
用戶表(PK USER_ID)
Fields: user_id, user_first_name, user_last_name, username, user_email...etc
好朋友表(FK user1_id,從PK USER_ID在user2_id用戶表)
pal_id user1_id user2_id status timestamp 7 98 97 0 2011-02-02 21:44:28 8 92 98 1 2011-02-04 08:06:00 9 95 92 0 2011-02-04 08:05:54 10 97 92 1 2011-02-04 08:05:28 11 97 95 1 2011-02-04 08:06:33
圖象表(從PK USER_ID FK USER_ID在用戶表)
picture_id picture_url picture_thumb_url user_id avatar timestamp
73 ../User_Images/66983.jpg ../User_Images/Thumbs/66983.jpg 92 0 2011-02-03 21:52:02
74 ../User_Images/56012.jpg ../User_Images/Thumbs/56012.jpg 92 0 2011-01-25 12:09:58
75 ../User_Images/58206.jpg ../User_Images/Thumbs/58206.jpg 95 0 2011-01-22 22:12:35
76 ../User_Images/85428.jpg ../User_Images/Thumbs/85428.jpg 98 0 2011-01-23 23:50:16
77 ../User_Images/42325.jpg ../User_Images/Thumbs/42325.jpg 98 0 2011-01-24 00:11:15
78 ../User_Images/73154.jpg ../User_Images/Thumbs/73154.jpg 98 1 2011-01-24 00:11:15
81 ../User_Images/92865.jpg ../User_Images/Thumbs/92865.jpg 92 0 2011-01-31 18:24:34
82 ../User_Images/75611.jpg ../User_Images/Thumbs/75611.jpg 92 0 2011-01-26 18:08:52
83 ../User_Images/74829.jpg ../User_Images/Thumbs/74829.jpg 95 0 2011-02-01 20:48:48
84 ../User_Images/5987.jpg ../User_Images/Thumbs/5987.jpg 92 1 2011-02-03 21:52:02
我正在製作一個社交網站,我希望在PHP生成的表格中顯示用戶的好友。我想在這些縮略圖下方顯示朋友縮略圖和其他信息,這樣如果您單擊縮略圖,它會將您帶到該用戶的個人資料。
從上面,用戶92與用戶98的好朋友,因爲他們已經證實友誼(示出爲狀態=「1」)
user1_id在帕爾斯得到友誼的發起者的用戶ID。對於pal_id = 8,用戶92請求友誼,95確認它。如果用戶95請求了友誼並且已經確認,則user2_id將會讀取92.用戶92具有另一個朋友,用戶97.97發起友誼。
用戶可以有圖片,存儲在上面的圖片表中。用戶從他的照片中選擇他的頭像...這是通過設置他的一個picture.avatar ='1'來完成的。
現在,我將如何讓我的PHP查詢顯示朋友縮略圖和信息?
我有這個迄今爲止(我想要的結果是路要走!)
<?php require_once('Connections/connections.php'); ?>
<?php
//query username
$user_id = $_SESSION['UserSession'];
$user_id = mysql_real_escape_string($user_id);
mysql_select_db($database_connections, $connections);
$query_user_info = "SELECT username FROM users WHERE user_id='$user_id'";
$user_info = mysql_query($query_user_info, $connections) or die(mysql_error());
$row_user_info = mysql_fetch_assoc($user_info);
bla bla bla
<?php
while ($pal_no = mysql_fetch_assoc($pal_no_result))
{
//get pal info
$query_pal_info = "SELECT * FROM users INNER JOIN pals ON (pals.user1_id = users.user_id) OR (users.user_id = pals.user2_id)
INNER JOIN picture ON picture.user_id = users.user_id AND picture.avatar = '1' WHERE users.user_id = '$user_id'";
$pal_info = mysql_query($query_pal_info , $connections) or die(mysql_error());
$totalRows_pal_info = mysql_num_rows($pal_info);
//echo table with pal information
echo "\n<table>";
$j = 5;
while ($row_pal_info = mysql_fetch_assoc($pal_info))
{
if($j==5) echo "\n\t<tr>";
$thumbnail_user = $row_pal_info['picture_thumb_url'] != '' ? $row_pal_info['picture_thumb_url'] : '../Style/Images/default_avatar.png';
echo "<td width='100' height='100' align='center' valign='middle'><a href = 'user_view.php?user_id2={$row_pal_info['user_id']}'>
<img src='/NNL/User_Images/$thumbnail_user' border='0'/></a></td>\n";
$j--;
if($j==0) {
echo "\n\t</tr>\n\t<tr>";
$j = 5;
}
}
if($j!=5) echo "\n\t\t<td colspan=\"$j\"></td>\n\t</tr>";
echo "\n</table>";
}
?>
澤工業區的問題在這裏:
$query_pal_info = "SELECT * FROM users INNER JOIN pals ON (pals.user1_id = users.user_id) OR (users.user_id = pals.user2_id)
INNER JOIN picture ON picture.user_id = users.user_id AND picture.avatar = '1' WHERE users.user_id = '$user_id'";
如何解決呢?先謝謝你們。
如果你仍然有問題,那麼你可以給我發送你的數據庫導出sql文件 – 2011-02-04 11:10:20
@experimentX。也許我在我的數據庫中有多餘的信息。你有我可以發送給的電子郵件地址? – 2011-02-04 14:03:21