@Diego是正確的,您不能使用SQL參數作爲表名,列名,SQL關鍵字,值列表(如IN()謂詞)或任何其他表達式。
SELECT :column FROM table -- NO, unless you want to select a constant string
SELECT * FROM :table -- NO
SELECT * FROM table WHERE column IN (:list) -- NO
SELECT * FROM table ORDER BY :column -- NO
SELECT * FROM TABLE ORDER BY column :asc_or_desc -- NO
基本上,記住這條規則:如果你能代替SQL參數的投入恆定值(例如帶引號的字符串,日期或整數),這是一個合法使用的參數。否則,不。
SELECT :string FROM table -- OK, but returns value of :string for every row
SELECT * FROM table WHERE column = :string -- OK
SELECT * FROM table WHERE column IN (:x, :y, :z) -- OK, one parameter per value
而且編程PDO時,您應經常檢查的prepare()
和返回值。他們將返回錯誤false
,你應該寫你的代碼來檢測這一點,並作出適當的反應(即日誌錯誤,顯示錯誤頁面,用戶給予一次機會,等)
$stmt = $conn->prepare("SELECT * FROM :type"); // illegal use of parameter
if ($stmt === false) {
// check $pdo->errorInfo(), see documentation
}
$stmt->bindParam(':type', $type);
$status = $stmt->execute();
if ($status === false) {
// check $stmt->errorInfo(), see documentation
}
你甚至可能要檢查的回報其他PDO功能的值。請參閱文檔,其中許多人錯誤地返回false
。
問題是? –
爲什麼功能不起作用,對不起。 – Ben