此代碼有效,但由於將GET參數連接到查詢中似乎不安全。我是串聯的,因爲我需要WHERE子句中的動態數量的參數,它可以是不同類型的(IN
,正常比較條件)。如何使用動態數量的參數來準備語句?
如何從不同類型的WHERE條件的動態數字中準備安全語句?
class myclass
{
public function index($where_clause = 1)
{
// db connection (using pdo)
$stm = $this->dbh->query("SELECT COUNT(amount) paid_qs FROM qanda $where_clause");
$ret = $stm->fetch(PDO::FETCH_ASSOC);
// do stuff
}
public function gen_where_clause()
{
$where_clause = '';
if (isset($_GET['c']) || isset($_GET['t']))
{
$where_clause = 'WHERE ';
if (isset($_GET['c']))
{
$where_clause .= 'cat = ' . $_GET['c'];
}
if (isset($_GET['t']))
{
if (isset($_GET['c']))
{
$where_clause .= $where_clause . ' AND '
}
$where_clause .= 'tag IN(' . $_GET['t'] . ')';
}
}
return $this->index($where_clause);
}
}
@AbraCadaver你爲什麼把我的問題標記爲重複?您提供的重複鏈接完全不同。 –
'$ where_clause。='cat =:cat';'或'$ where_clause。='cat =?';'等等......並在稍後準備。 – AbraCadaver
@AbraCadaver在你的情況下,我必須添加多個條件來準備和綁定值,這是我真實代碼中的噩夢。由於我有幾個參數。 –