Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is my PDO query safe from SQL injection [duplicate]

I'm fairly new to PDO and wondering if my query below is safe from SQL injection. I'll be using this method throughout the site if so.

    // 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());
}
like image 818
user2183216 Avatar asked Mar 18 '13 16:03

user2183216


1 Answers

Yes - parameterized queries are safe from injection when used in this way.

like image 118
Martin Avatar answered Oct 18 '22 17:10

Martin