[英]Binding parameter to mysqli database query
我得到这个:
警告:mysqli_stmt_bind_result()[function.mysqli-stmt-bind-result]:绑定变量数与准备好的语句中的字段数不匹配
我已经在代码中在其上方定义了$ 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);
}
在我看来,查询只有一个'?',因此它只需要一个(整数类型)值即可填写,我想我可以为它提供$ id。 救命?
您必须将每个列名称放在SELECT中,而不要使用*
通配符Documentation 。
例
SELECT `FirstName`,`LastName` FROM `tablename` WHERE `id` = ?
错误消息说Number of bind variables doesn't match number of fields in prepared statement
。
在行mysqli_stmt_bind_result($stmt, $first, $last);
您正在尝试将两个变量( $first
和$last
)绑定到$stmt
。 但是,从数据库中选择的变量数量必须等于您尝试赋给$stmt
的变量数量。 您可以如上所述进行操作。
边注:我建议引述`tables`
和`columns`
与反引号的名字`
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.