在PDO中使用bindParam [英] using bindParam with PDO

查看:66
本文介绍了在PDO中使用bindParam的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我花了几个小时来摸索这个代码. 对我来说没有意义,为什么它不起作用

I've been scratching my head over this code for a couple of hours.... Doesn't make sense to me why it doesn't work

$isCorrect =($question->correct_answer == $body->answer) ? 1:0;
// the values are all there.......
// echo $body->question . "\n"; //335
// echo $body->user . "\n";     //51324123
// echo $question->day . "\n"; //0
// echo $isCorrect . "\n";     //0

//but still the below part fails.
$db = getConnection();
$sql = "INSERT INTO `answers` (`id`, `question_id`, `user`, `day`, `is_correct`) VALUES (NULL, ':question', ':user', ':day', :is_correct)";
$stmt = $db->prepare($sql);  
$stmt->bindParam(":question_id", $body->question);
$stmt->bindParam(":user", $body->user);
$stmt->bindParam(":day", $question->day, PDO::PARAM_INT);
$stmt->bindParam(":is_correct", $isCorrect, PDO::PARAM_INT);
$stmt->execute();

给出此错误:

SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens

我正在计数4个令牌...我想念什么?显然我做错了.

I'm counting 4 tokens... what am I missing? Obviously I'm doing something wrong.

推荐答案

更改:

$stmt->bindParam(":question_id", $body->question);

收件人:

$stmt->bindParam(":question", $body->question);

您已在查询:question中使用过,但绑定了错误的键(:question_id).

You have use in query :question but binding with wrong key(:question_id).

这篇关于在PDO中使用bindParam的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆