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.

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

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化