INSERT使用PDO编写的语句致命错误

时间:2015-08-06 01:42:02

标签: php mysql pdo

我尝试使用这样的预处理语句插入值:

$dbh = new PDO("mysql:host=$hostname;dbname=$database", $username, $password);
$dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "INSERT INTO user_table (first_name, last_name) VALUES (:tname, :tname2)";
$stmt = $dbh->prepare($sql);
$stmt -> bindParam(':tname', 'John');
$stmt -> bindParam(':tname2', 'Smith');
$stmt -> execute();

然而,这是一个致命错误:" PHP致命错误:无法通过第53行" /Applications/MAMP/htdocs/live/test_create.php中的引用传递参数2;这是指这一行:$stmt -> bindParam(':tname', 'John');

造成这个问题的原因是什么?

1 个答案:

答案 0 :(得分:2)

使用bindParam时,必须通过引用传递。

使用bindValue代替您在此处尝试使用它的方式。

有关bindValue vs bindParam here

的更多信息

如果您坚持使用bindParam,则必须将其作为变量提供。因此,您可以使用$var1="John",然后使用$stmt->bindParam(':tname',$var1);

相关问题