PHP Prepared-Statement:绑定变量参数
PHP Prepared-Statement: binding variable parameters
我的数据库中的 varchar 有问题,它只显示 0 而不是过去写入的文本。我有一个 URL,其中包含一些参数,例如昵称、积分或游戏难度。
localhost/api.php?nickname=test&points=5&difficulty=3
这些参数得到一个api(见下面的代码)并将它们写入数据库。
<?php
$nickname = $_GET['nickname'];
$points = $_GET['points'];
$difficulty = $_GET['difficulty'];
$mysqli = new mysqli("localhost", "games", "123", "tictactoe");
if ($mysqli->connect_errno) {
die("Error");
}
/* Prepared statement, stage 1: prepare */
$sql ="insert into highscores (nickname, points, difficulty) values (?, ?, ?)";
if (!($stmt = $mysqli->prepare($sql))) {
die("Error");
}
/* Prepared statement, stage 2: bind and execute */
if (!$stmt->bind_param("isi", $nickname, $points, $difficulty)) {
die("Error");
}
if (!$stmt->execute()) {
die("Error");
}
mysqli_close($mysqli);
?>
但我的问题是:如果 api 将参数绑定到类似于 "test"
的字符串,为什么数据库中的所有 varchar 都具有值 0
?
id nickname points difficulty
1 0 5 3
2 0 5 3
3 0 5 3
4 0 5 3
5 0 5 3
6 0 5 3
7 0 5 3
数据库结构:
Column Type Null Standard Comments
id int(11) No
nickname varchar(20) No
points int(11) No
difficulty tinyint(4) No
我希望你能理解我的问题并能帮助我:)
您的绑定已反转。
$stmt->bind_param("isi", $nickname, $points, $difficulty)
表示 $nickname
和 $difficulty
是整数。不过,您的数据库 nickname
为 varchar
。
应该是:
$stmt->bind_param("sii", $nickname, $points, $difficulty)
你可以看到它记录在案 here。
Character Description
i corresponding variable has type integer
d corresponding variable has type double
s corresponding variable has type string
b corresponding variable is a blob and will be sent in packets
问题出在 bind_parm。您正在使用 "i" 表示整数。您必须使用 "s" 这正是您所需要的。
i corresponding variable has type integer
d corresponding variable has type double
s corresponding variable has type string
b corresponding variable is a blob and will be sent in packets
我的数据库中的 varchar 有问题,它只显示 0 而不是过去写入的文本。我有一个 URL,其中包含一些参数,例如昵称、积分或游戏难度。
localhost/api.php?nickname=test&points=5&difficulty=3
这些参数得到一个api(见下面的代码)并将它们写入数据库。
<?php
$nickname = $_GET['nickname'];
$points = $_GET['points'];
$difficulty = $_GET['difficulty'];
$mysqli = new mysqli("localhost", "games", "123", "tictactoe");
if ($mysqli->connect_errno) {
die("Error");
}
/* Prepared statement, stage 1: prepare */
$sql ="insert into highscores (nickname, points, difficulty) values (?, ?, ?)";
if (!($stmt = $mysqli->prepare($sql))) {
die("Error");
}
/* Prepared statement, stage 2: bind and execute */
if (!$stmt->bind_param("isi", $nickname, $points, $difficulty)) {
die("Error");
}
if (!$stmt->execute()) {
die("Error");
}
mysqli_close($mysqli);
?>
但我的问题是:如果 api 将参数绑定到类似于 "test"
的字符串,为什么数据库中的所有 varchar 都具有值 0
?
id nickname points difficulty
1 0 5 3
2 0 5 3
3 0 5 3
4 0 5 3
5 0 5 3
6 0 5 3
7 0 5 3
数据库结构:
Column Type Null Standard Comments
id int(11) No
nickname varchar(20) No
points int(11) No
difficulty tinyint(4) No
我希望你能理解我的问题并能帮助我:)
您的绑定已反转。
$stmt->bind_param("isi", $nickname, $points, $difficulty)
表示 $nickname
和 $difficulty
是整数。不过,您的数据库 nickname
为 varchar
。
应该是:
$stmt->bind_param("sii", $nickname, $points, $difficulty)
你可以看到它记录在案 here。
Character Description
i corresponding variable has type integer
d corresponding variable has type double
s corresponding variable has type string
b corresponding variable is a blob and will be sent in packets
问题出在 bind_parm。您正在使用 "i" 表示整数。您必须使用 "s" 这正是您所需要的。
i corresponding variable has type integer
d corresponding variable has type double
s corresponding variable has type string
b corresponding variable is a blob and will be sent in packets