我想从多个表中获取数据来构建通知提要。从多个表中选择数据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