为什么这会返回一行而不是几行?
问题描述:
为什么这返回一个值,当我直接查询数据库时,我得到多个值?的为什么这会返回一行而不是几行?
$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一个问题,你不能绑定列/表名,你必须在这个这个where ? = ?
更改为where your_columns_name = ?
line:
$statement = $sql->prepare('select * from items where user_id = (select id from user where ?=? limit 1)');
并取回所有记录使用:
$data = $statement->fetchAll(PDO::FETCH_ASSOC);
+0
@luis我认为你必须阅读我的答案,因为你的sql有一个错误,看看并更正你的代码 – 2015-04-05 03:26:27
酷!谢谢,你知道为什么我不能执行(array($ property,$ value))而不是bindParam吗?由于某种原因,似乎不起作用。 – luis 2015-04-04 06:56:45
请参阅#示例2,在此链接中http://php.net/manual/en/pdostatement.execute.php并尝试通过,如 – 2015-04-04 06:59:01
不起作用。 #example 3 – luis 2015-04-04 07:01:44