准备好的陈述给出错误

时间:2015-01-25 01:07:45

标签: php prepared-statement

我错过了什么?

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test";

$name = 'Samuel "L" Jackson';

$conn = new mysqli($servername, $username, $password, $dbname);
$stmt = $conn->prepare("INSERT INTO test2 (id, name) VALUES (?,  
?)");
$stmt->bind_param("is",'600' , $name);

$stmt->execute();
$stmt->close();
$conn->close();
?>

我收到以下错误:
无法通过C ......中的引用传递参数2 ......

1 个答案:

答案 0 :(得分:1)

bind_param接受两个或多个参数。第一个必须是标识SQL参数的数据类型的字符串。 The rest of the arguments must be variables that can be passed by reference'600'是常量,因此您无法通过引用传递它。

只需使用临时变量来解决该限制,例如:

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test";

$id = 600;
$name = 'Samuel "L" Jackson';

$conn = new mysqli($servername, $username, $password, $dbname);
$stmt = $conn->prepare("INSERT INTO test2 (id, name) VALUES (?, ?)");
$stmt->bind_param("is", $id, $name);

$stmt->execute();
$stmt->close();
$conn->close();
?>