要動態構建bind_param,我在其他SO帖子中發現了這個。PHP中動態mysqli bind_param參數的簡單解決方案是什麼?
call_user_func_array(array(&$stmt, 'bindparams'), $array_of_params);
有人可以用簡單的英語爲我打破這個?我特別失去了第一個參數是一個數組。
要動態構建bind_param,我在其他SO帖子中發現了這個。PHP中動態mysqli bind_param參數的簡單解決方案是什麼?
call_user_func_array(array(&$stmt, 'bindparams'), $array_of_params);
有人可以用簡單的英語爲我打破這個?我特別失去了第一個參數是一個數組。
array($stmt, 'bindparams')
是,識別物體$stmt
的方法bind_params,因爲PHP 5中,您不需要使用前面的&
的PHP的方式不再(和mysqli的是PHP 5,它看起來在舊毛刺後)。
你可以看到一個類似的例子here
所以
call_user_func_array(array($stmt, 'bindparams'), $array_of_params);
基本上意味着
$stmt->bind_params($array_of_params[0], $array_of_params[1] ... $array_of_params[N])
有一個更傻笑的方式來做到這一點。
創建這個準備好的聲明:
select * from mytable
where status = ? and (userid = ? or ?)
and (location = ? or ?)
order by `date` desc, time desc
limt ?
,並通過綁定這樣的ARGS:
$stmt = $mysqli->prepare([statement above]);
$stmt->bind_param("siiiii",
"active", $userid, $userid == "ALL",
$location, $location == "ALL",
$limit);
謂詞(user_id = ? or ?)
當USER_ID等於先被替換參數時,或將成爲真正的第二個替換參數是真的。
$user_id
當轉換爲int時,它將是數值的字符串表示形式的值,否則爲零。表達式$userid == "ALL"
將評估爲布爾值,該布爾值將被傳遞給bind_param
。我們不能告訴bind_param
參數是一個布爾值(格式字符串只能理解字符串,int,double和blob),所以bind_param會將布爾值轉換爲一個int值,這對我們很有用。
只要數據庫中的user_id或location_id不爲零,就沒問題。
+1這解決了我的一些其他問題:) – johnnietheblack 2009-04-16 15:34:43
據我所知,你不能通過例如 $userid == "ALL"
到一個mysqli-statement-Object的bind_param方法,因爲這個方法希望參數通過引用傳遞。顯然,這是不可能的,因爲表達評估「就地」的結果。
作爲一種變通方法,我改變該程序的第二部分,以
$userIdEmpty = $userid == "ALL";
$locationEmpty = $location = "ALL";
$stmt->bind_param("siiiii",
"active", $userid, $userIdEmpty,
$location, $locationEmpty,
$limit);
這樣,布爾操作的結果可以通過參考通過。
謝謝你...我可以告訴它現在是一個美好的一天現在我已經有了 – johnnietheblack 2009-04-16 15:33:33