php - How a placeholder in a prepared statement calls a value in an associative array?
问题描述
The code is already working, but I couldn't fully understand how the place-holders is calling the value of an associative array. I already read the documentation but without success to understand it.
$app['dtb'] -> insert('users', [
'name' => $_POST['name']
'age' => $_POST['age], ]);`
My insert method:
$sql = sprintf(
'insert into %s (%s) values (%s)',
$table,
implode(', ' , array_keys($parameters)),
':' . implode(', :' , array_keys($parameters))
);
So this would result on something like this:
insert into users (name) values (:name)
If i'm not using any bind_param
how it identify that :name
is the value of the key name
?
解决方案
bind_param
is a mysqli
function. That driver does not support named placeholders. You must be using PDO
, PDO
has a bindparam
function that is similar. It also allows you to just pass an array to the execute
function. This binds the values in the order they appear if unnamed placeholders were used (?
), or by the keys of the array if the placeholders were named (:...
). I'd guess you are passing $parameters
to the execute
function.
推荐阅读
- gcc - 分配中的 C++17 排序:仍未在 GCC 中实现?
- javascript - 来自 URL 的 JSON.parse() 问题
- php - 为什么php进程等待另一个?
- rust - 在使用相同变量时借用 mutable 两次
- php - PHP - 为每条记录发布复选框值
- django - 如何向 Django Queryset 添加额外的字段
- xamarin.android - 使用命令行在 Xamarin 分析中触发快照
- javascript - Javascript - 计算提交中连续单词的所有组合
- mysql - 从 Docker 容器运行 Spring Boot 应用程序时出错
- php - 从字符串的末尾开始,一旦到达字符组的第一次出现,如何停止捕获?