Home >Backend Development >PHP Tutorial >How Can I Use jQuery Ajax to Successfully Retrieve and Display Data from a MySQL Database?
Using jQuery Ajax to Retrieve Data from MySQL
Problem:
The provided jQuery Ajax code is unable to retrieve and display data from a MySQL table successfully.
Solution:
To effectively retrieve data from a MySQL table using jQuery Ajax, several modifications are necessary:
jQuery Ajax Code:
<script type="text/javascript" src="jquery-1.3.2.js"></script> <script type="text/javascript"> $(document).ready(function() { $("#display").click(function() { $.ajax({ //create an ajax request to display.php type: "GET", url: "display.php", dataType: "html", //expect html to be returned success: function(response){ $("#responsecontainer").html(response); //alert(response); } }); }); }); </script>
Display.php (PHP Code to Retrieve Data):
include("connection.php"); mysqli_select_db("samples",$con); $result=mysqli_query("select * from student",$con); echo "
Roll No | Name | Address | Stream | Status | "; while($data = mysqli_fetch_row($result)) { echo "
$data[0] | "; echo "$data[1] | "; echo "$data[2] | "; echo "$data[3] | "; echo "$data[4] | "; echo "
Additional Notes:
The above is the detailed content of How Can I Use jQuery Ajax to Successfully Retrieve and Display Data from a MySQL Database?. For more information, please follow other related articles on the PHP Chinese website!