为什么这会返回一行而不是几行?

Why is this returning one row instead of several?

为什么返回一个值,而我直接查询数据库却得到多个值?

    $statement = $sql->prepare
        ('select * from items where user_id = (select id from user where ?=? limit 1)');
    $statement->bindParam('s', $property);
    $statement->bindParam('s', $value);
    $statement->execute();

    $data = $statement->fetch(PDO::FETCH_ASSOC);
    echo json_encode($data);

而不是

 $data = $statement->fetch(PDO::FETCH_ASSOC);

尝试

 $data = $statement->fetchAll(PDO::FETCH_ASSOC);

您的 sql 中有一个问题,您无法绑定 columns/table 名称 您必须在这一行中将此 where ? = ? 更改为 where your_columns_name = ?

$statement = $sql->prepare('select * from items where user_id = (select id from user where ?=? limit 1)');

要获取所有记录,请使用:

$data = $statement->fetchAll(PDO::FETCH_ASSOC);