所以我有这个输入,我可以在其中输入用户名,它将使用$ _POST来获取我输入框中的内容但是我正在努力如何从我的数据库中获取所有相关的信息这个用户名并显示它?
<form action="ProcessPlayerSearch.php" method="POST">
<div class="input-group input-group-sm">
<input type="text" name="SearchUser" id="SearchUser" class="form-control">
<span class="input-group-btn">
<button type="submit" class="btn btn-info btn-flat" type="button">Search</button>
</span>
</div>
</h4>
</form>
这是我的ProcessPlayerSearch.php
function GetPlayerSearch(){
global $database;
$user = $_POST['Searchuser'];
$q = "SELECT * FROM NEWPlayerInfo WHERE player=$user";
$result = $database->query($q);
/* Error occurred, return given name by default */
$num_rows = mysqli_num_rows($result);
if(!$result || ($num_rows < 0)){
echo "User not found";
return;
}
if($num_rows == 0){
echo "User not found";
return;
}
for($i=0; $i<$num_rows; $i++){
mysqli_data_seek($result, $i);
$row=mysqli_fetch_row($result);
$uuid = $row[0]; //UUID
$player = $row[1]; //player
$kicks = $row[2]; //kicks
$bans = $row[3]; //bans
echo "$uuid<br>";
echo "$player<br>";
echo "$kicks<br>";
echo "$bans<br>";
}
}
我得到的错误是
mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in
然而我不明白为什么它会返回一个布尔值?因为我在输入中输入了一个字符串?任何帮助谢谢。
答案 0 :(得分:0)
由于您正在显示单个用户数据,因此使用for循环会产生一些开销。现在我们返回包含函数用户信息的数组:
function GetPlayerSearch($user){
global $database;
$user = $_POST['Searchuser'];
$q = "SELECT * FROM `NEWPlayerInfo` WHERE player='$user'";
$result = $database->query($q);
/* Error occurred, return given name by default */
$num_rows = mysqli_num_rows($result);
$res =mysqli_fetch_assoc($result);
$userinfo =array();
if($num_rows>1){
$userinfo[] = $res;
}else{
$userinfo[] = array();
}
return $userinfo;
}
现在搜索用户:
if(isset($_POST['SearchUser'])){
$user = isset($_POST['SearchUser'])? $_POST['SearchUser']:'';
$player = GetPlayerSearch($user);
}
显示单个玩家数据:
if(!empty($player)){ //you can also use array_filter()
echo $player['UUID'].'<br>';
echo $player['player'].'<br>';
echo $player['kicks'].'<br>';
}else{
echo 'No user';
}