为什么这会返回一个值,当我直接查询数据库时,我得到多个值?
$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);
答案 0 :(得分:2)
而不是
$data = $statement->fetch(PDO::FETCH_ASSOC);
试
$data = $statement->fetchAll(PDO::FETCH_ASSOC);
答案 1 :(得分:1)
您的sql中存在一个问题,您无法绑定列/表名称,您必须在此行中将此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);