简体   繁体   中英

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. I am unable to bring Whole data. I am getting only first row. Here is my code.

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..! from 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

<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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM