【问题标题】:PDO prepared statements bindingPDO 准备好的语句绑定
【发布时间】:2018-09-07 13:06:18
【问题描述】:

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

$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 是否用值替换了我的部分键?

那里有什么问题?

感谢您帮助我。

【问题讨论】:

    标签: mysql arrays pdo prepared-statement


    【解决方案1】:

    edit: 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);
    

    【讨论】:

    • 我相当肯定您不需要数组键中的:。我不确定 OP 的问题来自哪里,但事实并非如此。 (More Info)
    猜你喜欢
    • 1970-01-01
    • 2016-09-04
    • 2010-11-30
    • 1970-01-01
    • 2015-07-13
    • 2011-07-13
    • 2017-09-29
    相关资源
    最近更新 更多