Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从php中获取整个数据并使用AJAX在html中显示_Php_Html_Ajax - Fatal编程技术网

如何从php中获取整个数据并使用AJAX在html中显示

如何从php中获取整个数据并使用AJAX在html中显示,php,html,ajax,Php,Html,Ajax,我想从PHP中获取数据,并使用Ajax将其显示在表中。 我无法带来全部数据。我只坐第一排。 这是我的密码 script.js $(document).ready(function() { $.ajax({ url: 'fetch.php', data: "", success: function(data) { var data = $.parseJSON(data); console.log

我想从PHP中获取数据,并使用Ajax将其显示在表中。 我无法带来全部数据。我只坐第一排。 这是我的密码

script.js

$(document).ready(function() {
    $.ajax({
        url: 'fetch.php',
        data: "",
        success: function(data) {
            var data = $.parseJSON(data);
            console.log(data);
        }
    });  
}
这是我的PHP代码。。! 从fetch.php

<?php 
    require_once 'db.php';
    $fetch = "SELECT * FROM users";
    $result = $conn->query($fetch)->fetch_assoc();
    exit(json_encode($result));
?>

这是我的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>

名称
年龄
位置

获取_​assoc
仅获取一行,请改用
获取所有行

$result = $conn->query($fetch)->fetch_all();

fetch\u assoc
从记录集中取出一行。。。您想使用循环遍历所有记录。是否为每个记录使用??如果我删除fetch_assoc(),那么如何迭代并发送JSON中的所有tha数据并将其发送到AJAX?我不明白,先生。用fetchall代替。。。或者读一读关于在循环中填充数组的琐碎任务。