简体   繁体   English

如何使用AJAX从php获取全部数据并以html显示

[英]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.

相关问题 使用php和ajax从数据库中获取数据并显示到文本框 - Fetch data from database and display to a textbox using php and ajax 我们如何使用 ajax 从 PHP 的数据库中获取数据并显示在输入类型的值中? - How can we fetch data from database in PHP using ajax and display in value of input type? 我如何使用ajax从PHP中的一个表中显示的两个表的下拉列表中获取数据 - How can i fetch data at dropdown from two tables display in one table in PHP using ajax 如何从数据库中获取数据并在应用程序中使用AJAX显示它 - How to fetch data from database and display it using AJAX in an app 使用ajax,php和html将数据库中的数据显示到表中 - Display data from database into a table using ajax, php and html 如何使用PHP从MYSQL同时获取和显示数据 - How to fetch and display data at the same time from MYSQL using PHP 如何使用php中的下拉菜单从数据库中获取数据并将其显示在标签上? - How to fetch data from the database and display it to the labels using a dropdown in php? 如何从PHP中的Ajax获取数据 - How to fetch data from ajax in php 如何从数据库中获取数据并在PHP中显示? - How to fetch data from database and display it in PHP? 如何在同一 HTML 页面上显示 HTML GET 请求数据,使用单独的 PHP 文件来获取它? - How do I display the HTML GET request data on the same HTML page, using a separate PHP file to fetch it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM