我已經寫了一個函數,應該用數組中的值替換?的值,但是我無法讓它正常工作。PDO bindParam錯誤
public function exec($sql, Array $params = array()) {
if($query = $this->db->prepare($sql)) {
$x = 1;
if(count($params)) {
foreach($params as $param) {
$query->bindParam($x, $param);
$x++;
}
}
}
$query->execute();
print_r($query);
查詢:
$mysql->exec("SELECT * FROM test where id = ?", array(0));
輸出:
PDOStatement Object ([queryString] => SELECT * FROM test where id = ?)
你什麼錯誤? – Vallentin
沒有錯誤,但?沒有被取代。 – ethan476
'print_r($ query);'不會工作,因爲綁定查詢不存在於php中,因爲查詢/參數被單獨發送到mysql。見http://stackoverflow.com/questions/210564或http://stackoverflow.com/questions/1786322或http://stackoverflow.com/questions/11122573或http://stackoverflow.com/questions/530627-it/530705#530705。你可以使用['print_r($ query-> debugDumpParams());'](http://php.net/manual/en/pdostatement.debugdumpparams.php) – Sean