Basically with using PDO you need just parameterize your data to prevent SQL injections. Without parameterizing your database will be still available for injections. The sanitization you can implement by yourself before passing values into SQL statement
Simple example of parameterized PDO statement:
<?php
// Connecting to database with values defined in 'database.php'
include_once 'database.php';
$db = new PDO($DB_DSN, $DB_USER, $DB_PASSWORD);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Function that takes SQL statement and parameters for it
function query($sql, $params = []) {
$stmt = $db->prepare($sql);
// Simple preparing and binding parameters to PDO::prepare()
if (!empty($params)) {
foreach ($params as $key => $val) {
if (is_int($val)) {
$type = PDO::PARAM_INT;
} elseif (is_bool($val)) {
$type = PDO::PARAM_BOOL;
} else {
$type = PDO::PARAM_STR;
}
$stmt->bindValue(':'.$key, $val, $type);
}
}
// Executing SQL
$stmt->execute();
return $stmt;
}
// I declare here simple variables that I'm usually getting from AJAX POST method
$id = "1";
$email = "email@example.com";
// SQL statement
$sql = 'SELECT * FROM `users` WHERE id = :id AND email = :email LIMIT 1';
// Parameters that will pass
$params = ['id' => $id, 'email' => $email];
// Executing SQL and saving return in variable
$result = query($sql, $params);
// Returning result
return $result;
So with such practice hacker won't be allowed to see any other data except that are passed into variables and couldn't make injection in it
Further readings: