<?php
require_once 'connection.php';
$p_sql = "SELECT *
FROM pq_posts p
JOIN pq_users u ON p.userId = u.userId
JOIN pq_user_profiles up ON p.userId = up.userId
WHERE userId = :userId
ORDER BY timestamp DESC ";
$stmt = $dbConn -> prepare($p_sql);
$stmt->execute( array (":userId" => $_SESSION['userName']));
$posts = $stmt->fetchAll();
?>
注意: 我想显示来自数据库的结果,只有帖子(pq_posts)来自当前使用“userId”登录的用户。这是因为我正在为“管理员主页”创建一个页面......
我收到此错误:
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'userId' in where clause is ambiguous' in /home/CLASSES/natovmatiej/CST336/final_project/admin_postquotes.php:50 Stack trace: #0 /home/CLASSES/natovmatiej/CST336/final_project/admin_postquotes.php(50): PDOStatement->execute(Array) #1 {main} thrown in /home/CLASSES/natovmatiej/CST336/final_project/admin_postquotes.php on line 50
第 50 行:
$stmt->execute( array (":userId" => $_SESSION['userName']));
谢谢。
最佳答案
我想问题是在 WHERE
子句中你写了 userId
,但是你应该像 p.userId
或 那样指定表>u.userId
:
$p_sql = "SELECT
FROM pq_posts p
JOIN pq_users u ON p.userId = u.userId
JOIN pq_user_profiles up ON p.userId = up.userId
WHERE p.userId = :userId
ORDER BY timestamp DESC ";
关于php mySQL准备续集,只显示与登录用户的用户ID相关联的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23419525/