`mysqli` does not bind params the same way `PDO` does
Description
The following query work with PDO:
select `id`, `name`, `code`, `is_eu`, (select group_concat(`name` separator :a) `name` from `user` `_U_f9aae5fda8d8` where `country_id` = `country`.`id`) `user_names` from `country` where `id` = :b limit 0, 2
array(2) {
[":a"]=>
string(1) ","
[":b"]=>
int(3)
}
but the following query (with named params converted to numerical ones) does not work:
select `id`, `name`, `code`, `is_eu`, (select group_concat(`name` separator ?) `name` from `user` `_U_f9aae5fda8d8` where `country_id` = `country`.`id`) `user_names` from `country` where `id` = ? limit 0, 2
array(2) {
[1]=>
string(1) ","
[2]=>
int(3)
}
Resulted in this error:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version
for the right syntax to use near '?) `name` from `user` `_U_f9aae5fda8d8` where `country_id` = `country`.`id`)
`us' at line 1
But I expected this output instead:
no error
PHP Version
PHP 7.4, 8.0 MySQL Server 5.6, 8.0
Operating System
Windows, linux
reproduced in CI https://github.com/atk4/data/pull/952/commits
Unfortunately, there's nothing we can do here. I understand this might be a problem, but from mysqli perspective, it's not a bug. It works correctly. The real reason why you see a syntax error is because that SQL has a syntax error... the separator value is not considered to be an expression, but rather a string value that is part of the SQL itself. It cannot be parameterized, replaced with a variable or replaced with another data type. It's a strange "feature" of SQL design. The same way that you can't parameterize column names, you can't parameterize the separator.
PDO is using emulated prepared by default. The preparation happens on the client-side (PHP) and the baked SQL query is sent to the server. Because the default parameter binding type is a string, when the SQL is compiled by PDO, the syntax accidentally comes out right. If you use non-emulated prepares or if you type-hint the parameter to be an integer, you will also see a syntax error.
In theory, PDO could even allow you to bind column/table names, but it's not possible because it is using single quotes instead of double quotes (intentionally). When PDO send the query to the MySQL server, it sends it as a normal SQL query, not a prepared statement. PDO doesn't check the syntax, it is only performing regex substitution. This means that there's no way for PDO to inform the user that they can't parameterize SEPARATOR. It's just a lucky side-effect of the technology. You can parameterize something that should not be parameterized, because PDO is too dumb to tell you you can't.
I am going to close this issue as "won't fix" because I don't see anything to fix here. The mysqli extension is working correctly, and the behaviour of PDO can't and won't be fixed any time soon. (it's not the only thing that emulated prepares have a problem with).
makes sense, thank you for your comprehensive post ❤️