我正在尝试使用 bootstrap 表、php 和 javascript 显示数据库中的数据。我有这个代码:
index.html
<div class="panel-body">
<div class="row">
<div class="col-md-12">
<table id="table"
data-show-columns="true"
data-height="500">
</table>
</div>
</div>
</div>
javascript
var $table = $('#table');
$table.bootstrapTable({
url: 'list-farmers.php',
search: true,
pagination: true,
buttonsClass: 'primary',
showFooter: true,
minimumCountColumns: 2,
columns: [{
field: 'landID',
title: 'ID',
sortable: true,
},{
field: 'location',
title: 'Location',
sortable: true,
},{
field: 'surf_area',
title: 'Surface Area',
sortable: true,
},{
field: 'surf_unit',
title: 'Unit',
sortable: true,
},{
field: 'ownership',
title: 'Ownership',
sortable: true,
},{
field: 'soiltype',
title: 'Soil Type',
sortable: true,
}, ],
});
php
include 'dbconnect.php';
$sqltran = mysqli_query($con, "SELECT * FROM land where farmerID = 8")or die(mysqli_error($con));
$arrVal = array();
$i=1;
while ($rowList = mysqli_fetch_array($sqltran)) {
$name = array(
'num' => $i,
'landID'=>$rowList['landID'],
'location'=> $rowList['location'],
'surf_area'=> $rowList['surf_area'],
'surf_unit'=> $rowList['surf_unit'],
'ownership'=> $rowList['ownership'],
'soiltype'=> $rowList['soiltype']
);
array_push($arrVal, $name);
$i++;
}
echo json_encode($arrVal);
mysqli_close($con);
代码一定有问题,因为当我运行它时,表格和设计都在那里,但没有数据。
这是数据库中应该匹配的数据。
将其添加到您的 list-farmers.php 中,就在您的开始标签之后
header('Content-Type: application/json');
希望这有帮助。
是的!我找到原因了
正如大家所说,MIME_TYPE 是 application/html。所以我把 header('内容类型:application/json');
我的 dbconnect.php 是这样的:
$user = 'root';
$pass = '';
$db = 'farmingportal';
$con = mysqli_connect('localhost', $user, $pass, $db);
if($con){
echo 'success!';
}else {
echo 'not successful';
}
所以我只是删除了 if 语句..
我已经使用这里提到的例子很长时间了。使用 PHP 7.4、Bootstrap 3 和 JQuery 1.11.1。 自从切换到 PHP 8.1、Bootstrap 5 和 JQuery 3.5.1 后,数据不再显示在表中。 奇怪的是我在Ajax请求的响应中看到了数据库中的数据。所以这个调用是正确的。但是,数据不再显示在表中。 有人对此有什么想法吗? 非常感谢 罗宾