[英]Binding parameter to mysqli database query
I'm getting this: 我得到这个:
Warning: mysqli_stmt_bind_result() [function.mysqli-stmt-bind-result]: Number of bind variables doesn't match number of fields in prepared statement 警告:mysqli_stmt_bind_result()[function.mysqli-stmt-bind-result]:绑定变量数与准备好的语句中的字段数不匹配
I've defined $id and $link above this in my code, and have no trouble connecting to the database. 我已经在代码中在其上方定义了$ id和$ link,并且可以轻松连接到数据库。
$query = "select * from tablename where id = ?";
if ($stmt = mysqli_prepare($link, $query)) {
mysqli_stmt_bind_param($stmt, 'i', $id);
//execute statement
mysqli_stmt_execute($stmt);
// bind result variables
mysqli_stmt_bind_result($stmt, $first, $last);
// fetch values
mysqli_stmt_fetch($stmt);
echo "$first $last<br>";
// close statement
mysqli_stmt_close($stmt);
}
It seems to me that the query only has one '?', so it should only need one (integer typed) value to fill in, which I think I'm supplying with $id. 在我看来,查询只有一个'?',因此它只需要一个(整数类型)值即可填写,我想我可以为它提供$ id。 Help? 救命?
You have to put each column name in your SELECT rather than use the *
wildcard, Documentation . 您必须将每个列名称放在SELECT中,而不要使用*
通配符Documentation 。
Example 例
SELECT `FirstName`,`LastName` FROM `tablename` WHERE `id` = ?
The error said Number of bind variables doesn't match number of fields in prepared statement
. 错误消息说Number of bind variables doesn't match number of fields in prepared statement
。
In line mysqli_stmt_bind_result($stmt, $first, $last);
在行mysqli_stmt_bind_result($stmt, $first, $last);
you're trying to bind two variables ( $first
and $last
) to $stmt
. 您正在尝试将两个变量( $first
和$last
)绑定到$stmt
。 But the amount of selected variables from the database has to be equal to the amount of variables you're trying to assing to $stmt
. 但是,从数据库中选择的变量数量必须等于您尝试赋给$stmt
的变量数量。 You can do that as stated above. 您可以如上所述进行操作。
Side Note: I recommend quoted `tables`
and `columns`
names with backticks `
. 边注:我建议引述`tables`
和`columns`
与反引号的名字`
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.