1
我是相當新的PHP和MySQL。我試圖從PHP創建一個休息API,並且因爲我的服務器沒有安裝mysqlnd
,所以我必須使用bind_result
和fetch
。PHP bind_result和獲取多行(數組)
$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;
}
上面的代碼工作,但它一次只能返回1行信息。
例如,如果語句返回:
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
}
您要更換'在while循環的每次迭代$ detail'。你需要通過'$ detail [] [「a」] = $ a'來追加,等等。 – 2014-10-19 04:45:37
你在共享虛擬主機或你自己的盒子?有沒有辦法安裝mysqlnd? – DarthCaniac 2014-10-19 04:47:48