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 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改
  • ¥15 Windows 系统cmd后提示“加载用户设置时遇到错误”
  • ¥50 vue router 动态路由问题
  • ¥15 关于#.net#的问题:End Function
  • ¥15 无法import pycausal
  • ¥15 VS2022创建MVC framework提示:预安装的程序包具有对缺少的注册表值的引用
  • ¥15 weditor无法连接模拟器Local server not started, start with?
  • ¥20 6-3 String类定义