PDO 预处理语句绑定
PDO prepared statements binding
我有一个数据数组,例如这个:
$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 是否用值替换了我的部分密钥?
哪里出了问题?
谢谢你帮我。
编辑:Execute 确实适用于命名绑定,因此您可以像这样编辑 $data 数组:
$data = [':ver_weather' => $post["weathercondition"],
':ver_flash' => $post["flashintense"],
':ver_earth' => $post["earthrumble"]]
注意每个键开头的 :
下面是原来的回答...
我认为问题在于您正在尝试按名称进行绑定,但我认为 PDOStatement 不支持命名绑定。我建议您尝试以下方法:
$data = [$post["weathercondition"], $post["flashintense"], $post["earthrumble"]];
$sql = "INSERT INTO `database`.`table` (`weather`, `flash`, `earth`)
VALUES (?, ?, ?)";
$pdo->prepare($sql)->execute($data);
我有一个数据数组,例如这个:
$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 是否用值替换了我的部分密钥?
哪里出了问题?
谢谢你帮我。
编辑:Execute 确实适用于命名绑定,因此您可以像这样编辑 $data 数组:
$data = [':ver_weather' => $post["weathercondition"],
':ver_flash' => $post["flashintense"],
':ver_earth' => $post["earthrumble"]]
注意每个键开头的 :
下面是原来的回答...
我认为问题在于您正在尝试按名称进行绑定,但我认为 PDOStatement 不支持命名绑定。我建议您尝试以下方法:
$data = [$post["weathercondition"], $post["flashintense"], $post["earthrumble"]];
$sql = "INSERT INTO `database`.`table` (`weather`, `flash`, `earth`)
VALUES (?, ?, ?)";
$pdo->prepare($sql)->execute($data);