歡迎來到小豬圈!

2007-06-05

使用 PDO parameters 來避免 SQL injection

  • 在找 PDO 在 SQL injection 方面的資料時找到的網站 Cluesheet.com,開宗明義,第一句就講了:Do use PDO parameters over SQL values to avoid SQL injection.,可是沒說HOW and WHY…(汗)。
  • 所以小實驗了一下,發現 bindParam() 有像 mysql_real_escape_string() 的效果。
  • $dbh = new PDO(DB_DSN, DB_USER, DB_PASSWORD);
    $sql_find_repeat = 'SELECT COUNT(*) FROM `table_name` WHERE `col_name` = ?;';
    $sth = $dbh->prepare($sql_find_repeat);
    $sth->bindParam(1, $value, PDO::PARAM_STR);
    $sth->execute();
    

No comments:

Post a Comment

Comment Form Message

標籤分類

Blog Archive

Labels

Google Analytics Tracking Code

About Me

My photo
Keelung, R.O.C, Taiwan
一個不學無術、混吃等死的傢伙…