【发布时间】:2016-12-28 17:50:38
【问题描述】:
我正在尝试进行用户名搜索 [我似乎已经完成并且工作正常],但是当您搜索用户名时,将显示有关该帐户的信息。比如我搜索过virtualAnon,他的名字和first_name等信息会出现在他的用户名后面。
我尝试通过将$query = "SELECT username FROM users WHERE username like ? LIMIT 1"; 替换为$query = "SELECT * FROM users WHERE username like ? LIMIT 1"; 来修复它,但在我尝试之后,错误
mysqli_stmt::bind_result(): 绑定变量数不匹配 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();
};
};
?>
【问题讨论】:
-
if($_GET['keyword'] && !empty($_GET['keyword']))是不可取的。只需删除第一个检查并完全依赖第二个检查。这样您就可以防止生成通知。
标签: php mysqli prepared-statement