2015-12-04 122 views
1
$sql="select * from question_test where test_id='".$test_id."' and difficulty_level BETWEEN ".$_SESSION['difficulty_start']." and ".$_SESSION['difficulty_end']." and question_id NOT IN ('".implode("', '", array_map('mysqli_real_escape_string', $_SESSION['question_attempt']))."') order by rand()*favourability_level desc"; 

在運行上述代碼我得到一個錯誤:如何將2個參數傳遞給php中的array_map函數?

警告:mysqli_real_escape_string()期望的是2個參數,1 C中給出:\ XAMPP \ htdocs中\ exam.php上線359

回答

1

使用一個回調函數,而不是

function array_map_callback($a) 
{ 
    global $con; 
    return mysqli_real_escape_string($con, $a); 
} 

array_map('array_map_callback', $_SESSION['question_attempt']); 

其中$con是連接變量。

所以你$sql變量爲:

$sql="select * from question_test where test_id='".$test_id."' and difficulty_level BETWEEN ".$_SESSION['difficulty_start']." and ".$_SESSION['difficulty_end']." and question_id NOT IN ('".implode("', '", array_map('array_map_callback', $_SESSION['question_attempt']))."') order by rand()*favourability_level desc"; 

,或者你可以用array_walk

array_walk($_SESSION['question_attempt'], function(&$string) use ($con) { 
    $string = mysqli_real_escape_string($con, $string); 
}); 
+1

或'array_map(函數($ A)使用($ CON)去{...}, $ _SESSION [ 'question_attempt'])'。 –

+0

@Sougata哇..我加入到我的答案,你評論! – Thamilan

+0

非常感謝,你保存了我的理智,最後一天我已經沉浸於此。 –

相關問題