我想從多個表中獲取數據來構建通知提要。從多個表中選擇數據PHP/Mysql
我有3個表:用戶,user_notification和user_comments
當我點擊圖標來檢查的通知,我想顯示爲一個下拉只有換新的,所以在user_notification表我行「狀態」,id_from和id_owner可以是「新」或「old」,這樣我就知道要檢索什麼了,但是當我需要從這些通知中獲得信息時,我的問題就出現了,關於是誰的信息並且能夠顯示例如個人資料圖片,用戶名,comment_id等
這是我試圖讓HTML:
while ($row_notification = $result_load_notifications->fetch_assoc()) {
echo '<li class="dropdown-item">
<img src="'.$row_notification["avatar"].'" class="avatar-feed" alt="'.$row_notification["name"].'" /><p><strong>'.$row_notification["name"].'</strong> new <a href="https://sample.com/user/'.$_SESSION['alias'].'/#comment-'.$row_notification["id_comment"].'">comment</a> notification.<br>
<time class="date-post" datetime="'.$row_notification["date"].'"></time></p>
</li>';
echo '<li class="dropdown-divider"></li>';
}
這是PHP /的mysqli我想可以工作,但都沒有:
SELECT
*
FROM
user_notification n,
users u,
user_comments c
WHERE
n.status='new'
AND
n.id_owner=$session_id
AND
u.id_user=n.id_from
AND
c.id_owner=n.id_owner
在PHP:
$load_notifications = mysqli_query($conn, "SELECT * user_notification n, users u, user_comments c WHERE n.status='new' AND n.id_owner=$session_id AND u.id_user=n.id_from AND c.id_owner=n.id_owner");
該代碼只是不工作,我做錯了什麼? (我不是專家)我錯過了表之間的關係?
任何提示將不勝感激, 謝謝。
'SELECT * FROM user_notification n,....' –
你的SQL無效,你沒有指定'FROM'子句。此外,您的代碼容易受到SQL注入攻擊:**不要使用字符串連接或字符串插值來生成SQL!** *始終使用參數化查詢!* – Dai
它不起作用?你是開放的SQL注入。由於您使用的是mysqli,請利用[prepared statements](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php)和[bind_param](http://php.net/手動/ EN/mysqli的-stmt.bind-param.php)。 – aynber