PHP bind_result并获取多行(数组)


PHP bind_result and fetch multiple rows (array)

我对php和mysql相当陌生。我试图从php创建一个rest api,因为我的服务器没有安装mysqlnd,所以我必须使用bind_resultfetch

$stmt = $this->conn->prepare("SELECT * from d WHERE d.id = ?");
$stmt->bind_param("i", 1);
if($stmt->execute()){
    $stmt->bind_result($a, $b, $c);
    $detail = array();  
    while($stmt->fetch()){      
        $detail["a"] = $a;
        $detail["b"] = $b;
        $detail["c"] = $c;
    }
    $stmt->close();
    return $response;
} else {
    return NULL;
}

上面的代码可以工作,但一次只能返回一行信息。

例如,如果语句返回:

a    b    c
1   test  test
1   test1 test1

它只返回

a: 1
b: test1
c: test1

它应该在哪里:

{
    a: 1
    b: test
    c: test
},
{
    a: 1
    b: test1
    c: test1
}

如果要覆盖它们,可以执行以下操作:

$detail = array();  
while($stmt->fetch())
{           
    $temp = array():
    $temp["a"] = $a;
    $temp["b"] = $b;
    $temp["c"] = $c;
    $detail[] = $temp;
}

或者直接附加另一个维度:

$detail = array();  
while($stmt->fetch()) {           
    $detail[] = array('a' => $a, 'b' => $b, 'c' => $c);
      //   ^ add another dimension
}