簡體   English   中英

如何從第二個表中獲取會話值?

[英]How to get a session value from a second table?

我有兩個供用戶使用的表; 登錄表和用戶個人資料表。

我想將“ userprofiletable”中的值與另一個稱為“ posts”的表中的另一個值進行比較。 如果值相等,則顯示一個列表。

我有以下代碼。 問題在於它沒有將posts表中的值與用戶配置文件表中的會話值進行比較。

有人可以幫我嗎?

<?php
$limit = '5';
$dbreq = 'SELECT * FROM `posts` ORDER BY `pos` DESC';
$dbdata = mysql_query($dbreq);
while($dbval = mysql_fetch_array($dbdata))
{
  if (($dbval['city'] == $_SESSION['student_city'])) { //checks for last 4 accomodation
    if ($limit >= '1') {
      echo '<tr><td><a href="acomod.php?view='.$dbval['id'].'">'.$dbval['title'].'</a></td></tr>';
      $limit = $limit -'1';
    }
  }
}
?>

我也想獲取userprofiletable的值並將其發布在posts表中。 例如,當有人發布新帖子時。

您的帖子尚不清楚,但我認為這是您想要的:

<?php
$userid = 11542;//Sample uid. You will have to figure this out and set it.
$limit = 5;
$dbreq = "SELECT * FROM `posts` WHERE `userid`=".$userid." ORDER BY `pos` DESC LIMIT=".$limit.";";
$dbdata = mysql_query($dbreq);
while($dbval = mysql_fetch_array($dbdata))
{
  if (($dbval['city'] == $_SESSION['student_city'])) { //checks for last 4 accomodation
    echo '<tr><td><a href="acomod.php?view='.$dbval['id'].'">'.$dbval['title'].'</a></td></tr>';
  }
}
?>

問題尚不清楚,但可能有兩個答案:

要重現代碼,可以在一個sql查詢中執行:

$dbreq = 'SELECT *
FROM `posts`
WHERE city="'.mysql_real_escape_string($_SESSION['student_city']).'"
ORDER BY `pos` DESC
LIMIT 4'; 

但是,如果有兩個表,則需要“ LEFT JOIN”將posts表鏈接到userprofile表

$dbreq = 'SELECT p.*, u.*
FROM posts p
LEFT JOIN userprofiletable up ON p.UserID=up.UserID
WHERE up.city="'.mysql_real_escape_string($_SESSION['student_city']).'"
ORDER BY p.pos DESC
LIMIT 4';

(上表中的UserID是posts表和userprofiletable中鏈接兩者的字段的名稱。)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM