mysqli_stmt::bind_result(): 绑定变量的数量与 PHP 中准备好的语句中的字段数量不匹配
我正在尝试进行用户名搜索 [我似乎已经完成并正在工作] 但是当您搜索用户名时,会显示有关该帐户的信息.比如我搜索了virtualAnon,他的名字和first_name等信息就会出现在他的用户名后面.
I am trying to do a username search [which I seemingly finished and working as well] but when you've searched for the username, the information about the account will show up. For example, I've searched for virtualAnon, his name and information such as first_name will show up after his username.
我试图通过替换 $query = "SELECT username FROM users WHERE username like 来修复它?LIMIT 1";
to $query = "SELECT * FROM users WHERE username like ?LIMIT 1";
但在我尝试之后,错误
I've tried to fix it by replacing $query = "SELECT username FROM users WHERE username like ? LIMIT 1";
to $query = "SELECT * FROM users WHERE username like ? LIMIT 1";
but after I've tried that, the error
mysqli_stmt::bind_result(): 绑定变量数量不匹配PHP中准备好的语句中的字段数
mysqli_stmt::bind_result(): Number of bind variables doesn't match number of fields in prepared statement in PHP
出现.
这是用于获取用户名和数据库的 PHP 文件:
<?php
if($_GET['keyword'] && !empty($_GET['keyword']))
{
$conn = mysqli_connect('localhost','root','','loginsecure'); //Connection to my database
$keyword = $_GET['keyword'];
$search = $_GET['keyword'];
$keyword="%$keyword%";
$query = "SELECT * FROM users WHERE username like ? LIMIT 1";
# When I tried to SELECT *, It gives me the error of: Warning: mysqli_stmt::bind_result(): Number of bind variables doesn't match number of fields in prepared statement in ...fetch.php on line 22
$statement = $conn->prepare($query);
$statement->bind_param('s',$keyword);
$statement->execute();
$statement->store_result();
if($statement->num_rows() == 0) // so if we have 0 records acc. to keyword display no records found
{
echo '<div id="item">Sorry, but there is no user "'.$search.'" found in our database :(</div>';
$statement->close();
$conn->close();
}
else {
$statement->bind_result($name); # There is a error i'm encountering when I try to Select * from the line 8.
while ($statement->fetch()) //outputs the records
{
echo "<div id='item'><a href="../user/username.php?username=$name">$name</a></div>";
# It supposed to show more information about the user, by using $name['first_name'] or $name['last_name']
};
$statement->close();
$conn->close();
};
};
?>
推荐答案
您遇到的问题是 mysqli_stmt::bind_result
将尝试将结果集中的每一列绑定到一个变量.这意味着您需要与列相同数量的变量.如果有两列返回,则需要将它们绑定到两个变量.
The problem that you're getting is that mysqli_stmt::bind_result
will try to bind each column in the result set to a variable. Which means that you need the same amount of variables as you've got columns. If you've got two columns being returned, you need to bind them to two variables.
在$statement->bind_result($name);
中,你是说只有一列,所以将它绑定到$name
" 而您的查询(SELECT * FROM users WHERE username like ? LIMIT 1
)正在获取该表的所有列.
In $statement->bind_result($name);
, you're saying "There's only going to be one column, so bind it to $name
" whereas your query (SELECT * FROM users WHERE username like ? LIMIT 1
) is fetching all the columns for that table.
所以解决方案是在这个实例中只选择你想要的单数列.替换
So the solution is to only select the singular column you want in this instance. Replace
SELECT name
与
SELECT *
相关文章