問題描述
我正在嘗試進行用戶名搜索 [我似乎已經(jīng)完成并正在工作] 但是當您搜索用戶名時,會顯示有關(guān)該帳戶的信息.比如我搜索了virtualAnon,他的名字和first_name等信息就會出現(xiàn)在他的用戶名后面.
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(): 綁定變量數(shù)量不匹配PHP中準備好的語句中的字段數(shù)
mysqli_stmt::bind_result(): Number of bind variables doesn't match number of fields in prepared statement in PHP
出現(xiàn).
這是用于獲取用戶名和數(shù)據(jù)庫的 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
將嘗試將結(jié)果集中的每一列綁定到一個變量.這意味著您需要與列相同數(shù)量的變量.如果有兩列返回,則需要將它們綁定到兩個變量.
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.
所以解決方案是在這個實例中只選擇你想要的單數(shù)列.替換
So the solution is to only select the singular column you want in this instance. Replace
SELECT name
與
SELECT *
這篇關(guān)于mysqli_stmt::bind_result(): 綁定變量的數(shù)量與 PHP 中準備好的語句中的字段數(shù)量不匹配的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!