把php查询放在数组中

时间:2014-06-04 08:36:49

标签: php mysql arrays

我有点问题,我有一个查询,我希望结果会出现在数组中。

function disponibilita($data) {
    $this->sql_open();
    $db=$this->db;

    $sql="SELECT id FROM turni WHERE date='$data'";$stmt=$db->prepare($sql);
    $stmt->execute();

    $stmt->bind_result($id);

    while ($stmt->fetch()) {
        printf ("%s", $id);
    }
}   `

printf标记正确的结果,如何将这个dbquery的结果放入数组?

1 个答案:

答案 0 :(得分:1)

查看PDO的PDOStatement::fetchAll ,或查看mysqli_result::fetch_all获取mysqli

function disponibilita($data) {
    $this->sql_open();
    $db=$this->db;
    $sql="SELECT id FROM turni WHERE date='$data'";
    $stmt=$db->prepare($sql);
    $stmt->execute();

    $results = $stmt->fetchAll(PDO::FETCH_ASSOC); // for PDO
    $results = $stmt->fetch_all(MYSQLI_ASSOC); // for mysqli

    return $results;
}