2009-07-05 26 views
1

以下是我用於從我的表中檢索多個數據的函數的代碼,但是我想根據字段數自動生成bind_result($ array [0],...,..)我在查詢中選擇。PHP和MYSQLi - 使用循環綁定參數並存儲在數組中?

例如..

$query=select a,b,c,d,e from table;//selecting 5 fields 
...... 
$stmt->execute();$stmt->bind_result($retrieve[0],$retrieve[1],$retrieve[2],$retrieve[3],$retrieve[4]); 

(5個值bind_result應自動生成) 幫助將不勝感激...謝謝

$query="SELECT comment, userid,UNIX_TIMESTAMP(dtime) 
       FROM comment_updates 
       WHERE updateid=46546 
       ORDER BY dtime DESC 
       LIMIT 10 "; 
     if($stmt = $this->conn->prepare($query)) { 
      $stmt->execute(); 
      $stmt->bind_result($comments[0],$comments[1],$comments[2]); 
      $i=0; 
      while($stmt->fetch()){ 
      $i++; 
      $name='t'.$i; 
      $$name = array($comments[0],$comments[1],$comments[2]); 
      } 
      return array($i,$t1,$t2,$t3,$t4,$t5,$t6,$t7,$t8,$t9,$t10); 
      $stmt->close(); 
     } 

回答

1

這應該讓你開始:

http://php.net/manual/en/mysqli-stmt.result-metadata.php

這將通過mysqli_num_fields()得到結果集中的字段數。

這應該是您的$retrieve陣列的大小。

由於bind_result不採取數組作爲參數,你需要使用call_user_func_array來實現這一目標:

call_user_func_array(array($stmt, 'bind_result'), $retrieve_references); 

$retrieve_references應該是在$retrieve元素引用數組。在call_user_func_array中使用$retrieve本身將觸發錯誤。