因此,我正在使用php,jquery和mysql进行PHP在线搜索。 尽管数据库中存在数据,但正在搜索的数据仍未显示在我的页面中。
rowCount()显示正确的行总数, 但页面未显示所有行。
这是我的PHP代码
<?php
if(isset($_POST['keyword'])){
$key = $_POST['keyword'];
$findPerusahaan = "SELECT * FROM perusahaan_instansi WHERE badan_hukum LIKE '%$key%' OR merek LIKE '%$key%' ";
$stmt = $conn->prepare($findPerusahaan);
$stmt->execute();
$data =$stmt->fetch();
foreach($stmt as $row){
echo $row['badan_hukum']."<br>";
}
//print_r($stmt);
echo $stmt->rowCount();
}
?>
这是页面
<input onkeyup="myFunction()" type="text" id="myInput" class="form-control"
id="perusahaan" name="perusahaan" placeholder="PT....." required>
<div id="perusahaan"></div>
<script>
function myFunction() {
console.log($('#myInput').val());
var keyword = $('#myInput').val();
$.ajax({
type:'POST',
url: 'index.php?modul=form_submit&action=perusahaan',
data: {'keyword': keyword },
success:function(data){
console.log(data);
$('#perusahaan').empty();
$('#perusahaan').append(data);
}
});
}
</script>
答案 0 :(得分:1)
$data
包含您的行数组,而不是$stmt
!
更改此:
$data = $stmt->fetch();
foreach($stmt as $row){
echo $row['badan_hukum']."<br>";
}
对此:
$data = $stmt->fetchAll();
foreach($data as $row){
echo $row['badan_hukum']."<br>";
}
答案 1 :(得分:-1)
尝试使用'%".$key."%'
代替'%$key%'
:
<?php
if(isset($_POST['keyword'])){
$key = $_POST['keyword'];
$findPerusahaan = "SELECT * FROM perusahaan_instansi WHERE badan_hukum LIKE '%".$key."%' OR merek LIKE '%".$key."%' ";
$stmt = $conn->prepare($findPerusahaan);
$stmt->execute();
$data =$stmt->fetch();
foreach($stmt as $row){
echo $row['badan_hukum']."<br>";
}
//print_r($stmt);
echo $stmt->rowCount();
}