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

时间:2015-04-04 06:53:49

标签: php mysql sql data-binding

为什么这会返回一个值,当我直接查询数据库时,我得到多个值?

    $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);

2 个答案:

答案 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);