pdo预处理语句绑定

vojdkbi0  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(537)

我有一个数据数组,例如:

$data = ['ver_weather' => $post["weathercondition"],
         'ver_flash'   => $post["flashintense"],
         'ver_earth'   => $post["earthrumble"]]

我在sql中使用的这些数据如下:

$sql = "INSERT INTO `database`.`table` (`weather`, `flash`, `earth`)
        VALUES (:ver_weather, :ver_flash, :ver_eart)";

$pdo->prepare($sql)->execute($data);

结果是:

INSERT INTO `database`.`table` (`weather`, 'flash', `earth')
VALUES ('1'weather, '1'flash, '1'earth)

那么pdo是否正在用值替换我的部分键呢?
那里怎么了?
谢谢你帮我。

vxbzzdmp

vxbzzdmp1#

edit:execute与命名绑定一起工作,因此您可以这样编辑$data数组:

$data = [':ver_weather' => $post["weathercondition"],
         ':ver_flash'   => $post["flashintense"],
         ':ver_earth'   => $post["earthrumble"]]

注意:在每个键的开头
下面是原始答案。。。
我认为问题是您试图通过名称绑定,而pdo语句不支持命名绑定。我建议尝试以下方法:

$data = [$post["weathercondition"], $post["flashintense"], $post["earthrumble"]];
$sql = "INSERT INTO `database`.`table` (`weather`, `flash`, `earth`)
        VALUES (?, ?, ?)";

$pdo->prepare($sql)->execute($data);

相关问题