I was reading about the second order MySQL injection on this page Are PDO prepared statements sufficient to prevent SQL injection?.
and it brought many questions about the charset
, and I am not sure if my code is safe to MySQL injection
In my code, I never use charset while making a query,
I simply do
$pdo = new PDO("mysql:host=" . DB_HOST . ";dbname=" . DB_NAME, DB_USER, DB_PASSWORD, [PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_EMULATE_PREPARES => false, PDO::ATTR_PERSISTENT => false]);
$stmt = $pdo->prepare("SELECT * FROM keywords WHERE keyword_name = ? || keyword_name = ?");
$stmt->execute(["hello","world"]);
rows = $stmt->fetchAll();
// show the data on webpage
$pdo = null;
I found there are two different ways to set
the charset
in pdo
$pdo = new PDO("mysql:host=" . DB_HOST . ";charset=utf8;......);
and
$pdo->exec("set names utf8");
According to @ircmaxell answers on this link Are PDO prepared statements sufficient to prevent SQL injection?. the first method should be used to protect against second-order SQL injection...
But I had never set
the charset
in my codes (as shown in first code) so I have a few questions
- for the first code where I am not setting any charset, what would the Default charset, and would it be safe?
- is it related to the charset of the database, for my database charset (Collation) is
ut8_general_ci
(found that in phpmyadmin->operations)? - is
utf8
charset in safe for second-order injection i.e$pdo = new PDO("mysql:host=" . DB_HOST . ";charset=utf8;......);
is done job against all kind of mysql injections?