Auto-Wrapping Placeholders in Quotes in PDO in PHP
Technically, PDO wraps all inserted values in quotes, considering them strings, even if they are actually numbers. This is allowed by SQL syntax. Therefore, placeholders do not need to be wrapped in quotes if you have a string value.
Let's look at an example. Let's say we have a string stored in a variable:
<?php
$name = 'name1';
?>
A positional placeholder does not need to be wrapped in a string in this case:
<?php
$sql = 'SELECT * FROM users WHERE name=?';
$res = $pdo->prepare();
$res->execute([$name]);
?>
A named placeholder also does not need to be wrapped in a string:
<?php
$sql = 'SELECT * FROM users WHERE name=:name';
$res = $pdo->prepare();
$res->execute(['name' => $name]);
?>
The fact that queries will always contain strings can sometimes lead to problems with SQL syntax - in places where the syntax strictly requires numbers. In this case, you can use an alternative method of binding parameters, which we will cover in the following lessons.