matlabmann 2015-08-13 07:13
浏览 51
已采纳

PDO错误代码HY093,但绑定变量的数量与令牌的数量匹配[重复]

This question already has an answer here:

I have the following php function and have checked it over countless times.

I have looked at about 30 different questions abouts the HY903 error code. However, none of them seem to apply.

I have checked the number of columns, with the number of placeholders, I have checked that the spelling is consistent. I have even enclosed values that could be null in if(!is_null){} but nothing seems to shift this error. I have to be missing something small, but I can't for the life of me find out what it is.

function add_page($title, $linkStub, $content,$gitTitle,$buttonLabel,$buttonLink,$buttonText,$buttonTarget)
{
    $sql = "INSERT INTO `pages`(
            `pageId`,
            `title`,
            `linkStub`,
            `content`,
            `gitTitle`,
            `buttonLabel`,
            `buttonLink`,
            `buttonText`,
            `buttonTarget`
            )
            VALUES (
            NULL ,
            :title ,
            :linkStub ,
            :content ,
            :gitTitle ,
            :buttonLabel ,
            :buttonLink,
            :buttonText,
            :buttonTarget
            )";

    $stmt = $this->db->prepare($sql);

    $stmt->bindParam(':title', $title, PDO::PARAM_STR, 100);
    $stmt->bindParam(':linkStub', $linkStub, PDO::PARAM_STR, 150);
    $stmt->bindParam(':content', $content, PDO::PARAM_STR);
    if(!is_null($gitTitle))
    {
        $stmt->bindParam(':gitTitle', $gitTitle, PDO::PARAM_STR, 150);
    }
    if(!is_null($buttonLink))
    {
        $stmt->bindParam(':buttonLabel', $buttonLabel, PDO::PARAM_STR, 150);
        $stmt->bindParam(':buttonLink',  $buttonLink, PDO::PARAM_STR, 150);
        $stmt->bindParam(':buttonText', $buttonText, PDO::PARAM_STR, 100);
        $stmt->bindParam(':buttonTarget', $buttonTarget, PDO::PARAM_INT, 2);
    }
    $bool = $stmt->execute();
    print_pre($stmt->errorInfo()); //Uncomment for error reporting
    return $bool;
}

EDIT:

This question is not a duplicate of the ones you marked these issues there were incorrect amount of bindings, this is dealing with NULL values which Robbie Averill kindly pointed out.

</div>
  • 写回答

1条回答 默认 最新

  • dssk35460 2015-08-13 07:18
    关注

    You only bind some of the parameters if their values aren't null. You should handle your errors before you execute the statement, rather than executing anyway, or just bind null.

    PDO won't execute successfully with unbound parameters, so that is likely why you are getting that error.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源