这是我的问题...我正在尝试返回多行而不使用“喜欢”子句从我的 PDO 语句中刷新页面,问题是它只返回一行而不是其余的...有人可以帮我吗? 提前致谢
这是我的html表单:
<h2>Please insert the username you would like to search for</h2>
<div align="center" id="loader_div"><span id="search_result"></span></div>
<form action="send/search.php" method="post" id="search_form">
<input type="text" id="search_username" name="get_name" />
<input type="submit" name="submitsearch" />
</form>
<div id="get_users">
</div>
我的PHP如下:
$search = $_POST['get_name'];
$query = $db->prepare("SELECT *
FROM `users`
WHERE `users`.`username` LIKE ? LIMIT 10");
$query->bindValue(1, "%".$search."%", PDO::PARAM_STR);
try {
$query->execute();
$data['success'] = true;
while($row = $query->fetch(PDO::FETCH_OBJ)) {
$data['users'] = " ".$row->username." ";
echo json_encode($data);
exit();
}
} catch (PDOException $e) {
die($e->getMessage());
exit();
}
这是我的 jQuery 返回 PHP 结果:
$.ajax ({
type: "POST",
url: "send/search.php",
data: $('#search_form').serialize(),
dataType: "json",
success: function(data){
if(data.success === true)
{
$("#display_users").html(data.users);
},
error: function(xhr, status, et) {
}
});