我對PDO相當陌生,想知道下面的查詢是否是從SQL注入安全的。如果是的話,我會在整個網站中使用這種方法。我的PDO查詢安全嗎SQL注入
// make connection to DB
$db = new PDO('mysql:host='.$dateBaseHost.';dbname='.$dateBaseName, $dateBaseUsername, $dateBasePassword);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
//simple query and binding with results
$query = $db->prepare(" SELECT * FROM `profile` WHERE `fullname` = :fullname ");
$search = (isset($_GET['search']) === true) ? $_GET['search'] : '' ; // ? : shorthand for if else
// bind parameters - avoids SQL injection
$query->bindValue(':fullname', $search);
//try... if not catch exception
try {
// run the query
$query->execute();
$rows = $query->fetchAll(PDO::FETCH_ASSOC);
echo '<pre>', print_r($rows, true),'</pre>';
}
catch (PDOException $e){
sendErrorMail($e->getMessage(), $e->getFile(), $e->getLine());
}
只要您使用參數化,查詢您的查詢是安全的。 – Rohitink 2013-03-18 16:36:03
是的。您正在使用參數化,它將自動進行必要的轉義。好東西! – halfer 2013-03-18 16:36:17