2011-02-25 104 views
0

我有以下腳本:mysqli的編制聲明NUM_ROWS多參數

<?php 
$mysqli = new mysqli('localhost', 'user', 'password', 'database'); 

$statement = $mysqli->stmt_init(); 

$query = 'SELECT * FROM table WHERE id = ? AND active = 1'; 
$statement->prepare($query); 
$parameters = array('i'); 
$inputParameters = array(10); 
foreach ($inputParameters as $param) { 
    $parameters[] =& $param; 
} 
call_user_func_array(array($statement, 'bind_param'), $parameters); 
$statement->execute(); 
$statement->store_result(); 
echo $statement->num_rows; 
?> 

這正好返回的行權數量。

但是,當我改變了腳本:

<?php 
$mysqli = new mysqli('localhost', 'user', 'password', 'database'); 

$statement = $mysqli->stmt_init(); 

$query = 'SELECT * FROM table WHERE id = ? AND active = ?'; 
$statement->prepare($query); 
$parameters = array('ii'); 
$inputParameters = array(10, 1); 
foreach ($inputParameters as $param) { 
    $parameters[] =& $param; 
} 
call_user_func_array(array($statement, 'bind_param'), $parameters); 
$statement->execute(); 
$statement->store_result(); 
echo $statement->num_rows; 
?> 

則返回0。有沒有人有這一個解釋?對我來說,只要有超過1個參數綁定到語句,num_rows就會停止工作。

p.s:在完整的腳本中有一個理由在這裏使用call_user_func_array,而不是使用call_user_func_array給出了相同的結果。

回答

1

經過大量調試,我找到了答案:$parameters將在第二個代碼中爲array('ii', 1, 1)。這是因爲那裏使用的參考。將foreach ($inputParameters as $param) {更改爲foreach ($inputParameters as &$param) {修復了問題