2014-10-19 65 views
1

我是相当新的PHP和MySQL。我试图从PHP创建一个休息API,并且因为我的服务器没有安装mysqlnd,所以我必须使用bind_resultfetchPHP 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 
} 
+0

您要更换'在while循环的每次迭代$ detail'。你需要通过'$ detail [] [“a”] = $ a'来追加,等等。 – 2014-10-19 04:45:37

+0

你在共享虚拟主机或你自己的盒子?有没有办法安装mysqlnd? – DarthCaniac 2014-10-19 04:47:48

回答

1

你overwritting他们,你可以这样做像这样的代替:

$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 
}