[英]How to fetch whole data from php and display it in html using AJAX
I want to fetch data from PHP and display it in the table using Ajax. 我想从PHP提取数据并使用Ajax将其显示在表中。 I am unable to bring Whole data. 我无法带来整体数据。 I am getting only first row. 我只得到第一行。 Here is my code. 这是我的代码。
script.js 的script.js
$(document).ready(function() {
$.ajax({
url: 'fetch.php',
data: "",
success: function(data) {
var data = $.parseJSON(data);
console.log(data);
}
});
}
Here is my PHP code..! 这是我的PHP代码。 from fetch.php 从fetch.php
<?php
require_once 'db.php';
$fetch = "SELECT * FROM users";
$result = $conn->query($fetch)->fetch_assoc();
exit(json_encode($result));
?>
And here is my HTML 这是我的HTML
<table>
<tr>
<th>NAme</th>
<th>Age</th>
<th>Location</th>
</tr>
<?php foreach ($all as $key) { ?>
<tr>
<td><?php echo $key['username']; ?></td>
<td><?php echo $key['age']; ?></td>
<td><?php echo $key['location']; ?></td>
</tr>
<?php }?>
</table>
fetch_assoc
只获取一行,请改用fetch_all
:
$result = $conn->query($fetch)->fetch_all();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.