Home >Backend Development >PHP Tutorial >How to Efficiently Retrieve MySQL Data Using jQuery AJAX?

How to Efficiently Retrieve MySQL Data Using jQuery AJAX?

Susan Sarandon
Susan SarandonOriginal
2024-12-03 08:20:09363browse

How to Efficiently Retrieve MySQL Data Using jQuery AJAX?

Retrieving Data from MySQL Using Jquery Ajax

If you want to display only records from a MySQL table using Ajax and Jquery, there's a more efficient approach to your current code:

list.php:

<html>
<head>
    <script src="jquery-1.9.1.min.js"></script>
    <script>
    $(document).ready(function() {
        var response = '';
        $.ajax({
            type: "GET",
            url: "Records.php",
            async: false,
            success: function(text) {
                $("#div1").html(text);
            }
        });
    });
    </script>
</head>

<body>
    <div>

Records.php:

Your code for fetching records from MySQL is generally correct. However, to make it work with the updated list.php, you need a few adjustments:

<?php
$con = mysqli_connect("localhost","root","");
$dbs = mysqli_select_db("simple_ajax",$con);
$result = mysqli_query("select * from users");

echo "<table border='1'>";
echo "<tr><th>Name</th><th>Address</th></tr>";
while ($row = mysqli_fetch_array($result)) {
    echo "<tr><td>$row[1]</td><td>$row[2]</td></tr>";
}
echo "</table>";
?>

In this script, we've enclosed the output in HTML table tags and removed the unnecessary PHP tags.

To use the mysqli connection, you can also modify your code as follows:

$con = mysqli_connect("localhost","root","");
$dbs = mysqli_select_db("simple_ajax",$con);

The above is the detailed content of How to Efficiently Retrieve MySQL Data Using jQuery AJAX?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn