2017-08-24 72 views
0

我在app/Lib中有一個幫助程序庫,它需要存儲日誌,因此我正在嘗試DboSource::rawQuery()。 Docs非常模糊:使用預處理語句的原始插入查詢

/** 
* Executes given SQL statement. 
* 
* @param string $sql SQL statement 
* @param array $params Additional options for the query. 
* @return bool 
*/ 
    public function rawQuery($sql, $params = array()) { 
     $this->took = $this->numRows = false; 
     return $this->execute($sql, $params); 
    } 

...以及我在網上找到的任何示例都假裝SQL注入不存在。不管是什麼語法我嘗試:

public function log (DataSource $db) { 
    $sql = 'INSERT INTO log (foo, bar) 
     VALUES (:foo, :bar)'; 
    $params = array(
     'foo' => 'One', 
     'bar' => 'Two', 
    ); 
    $db->rawQuery($sql, $params); 
} 

...我總是得到數據庫錯誤是這樣的:

SQLSTATE [42000]:[微軟] [ODBC SQL Server的驅動程序11] [SQL服務器]':'附近的語法錯誤。

我也試過位置佔位(?),甚至PDO樣PARAMS(與PDO::PARAM_STR和所有的東西)。

在CakePHP/2.5.5中針對SQL Server運行參數化原始查詢的語法是什麼?

回答

0

答案是(顯然):無。

source code for Sqlserver::_execute(),我們可以讀到:

/** 
* Executes given SQL statement. 
* 
* @param string $sql SQL statement 
* @param array $params list of params to be bound to query (supported only in select) 
* @param array $prepareOptions Options to be used in the prepare statement 
* @return mixed PDOStatement if query executes with no problem, true as the result of a successful, false on error 
* query returning no rows, such as a CREATE statement, false otherwise 
* @throws PDOException 
*/ 

所以你只能用在讀書準備的語句,而不是在寫: - !

你需要回到21世紀初和逃生

public function log (DataSource $db) { 
    $sql = sprintf('INSERT INTO log (foo, bar) 
     VALUES (%s, %s)', 
     $db->value('foo'), 
     $db->value('bar') 
    ); 
    $db->rawQuery($sql); 
}