php mySQL准备续集,只显示与登录用户的用户ID相关联的结果

标签 php mysql sql

<?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/

相关文章:

mysql - 如何比较两个表之间的10个字段

sql - 在存储过程中将查询作为参数传递

php - Laravel NotFoundHttpException 路由

php - Symfony Serializer 无法在反序列化时处理 Doctrine Id

PHP PDO查询问题

sql - 在关系数据库中存储矩阵

php - 在数据库中插入 1 次但 2 条记录

php - PHP 中的契约式编程

php - php 的(和 python 的)散列函数与我的系统不同的原因是什么?

Mysql RAM 使用率高