Home > Database > Mysql Tutorial > How to Properly Retrieve MySQL Data Using jQuery AJAX and Address Deprecated PHP Functions?

How to Properly Retrieve MySQL Data Using jQuery AJAX and Address Deprecated PHP Functions?

Linda Hamilton
Release: 2024-12-09 04:19:10
Original
788 people have browsed it

How to Properly Retrieve MySQL Data Using jQuery AJAX and Address Deprecated PHP Functions?

Using jQuery AJAX to Retrieve Data from MySQL

Retrieving data from a MySQL database using jQuery AJAX is a common task in web development. However, there may be instances where the code fails to work as intended.

One such instance is when attempting to display records from a MySQL table via an Ajax call. The provided code snippet:

Records.php:

<?php
    //database name = "simple_ajax"
    //table name = "users"
    $con = mysql_connect("localhost","root","");
    $dbs = mysql_select_db("simple_ajax",$con);
    $result= mysql_query("select * from users");
    $array = mysql_fetch_row($result);
?>
Copy after login

and

list.php:

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

        alert(response);
    });
    </script>
</head>

<body>
    <div>
Copy after login

is not functioning as expected. The issue may lie in the use of deprecated PHP functions. To resolve this, the code should be updated to use mysqli_connect instead of mysql_connect, mysqli_select_db instead of mysql_select_db, and mysqli_query instead of mysql_query.

Furthermore, for retrieving data using Ajax jQuery, the following code snippet can be used:

<html>
<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>

<body>
<h3>Manage Student Details</h3>
<table border="1" align="center">
   <tr>
       <td> <input type="button">
Copy after login

For MySQLi connection, use the following code:

<?php
$con=mysqli_connect("localhost","root","");
?>
Copy after login

To display data from the database:

<?php
include("connection.php");
mysqli_select_db("samples",$con);
$result=mysqli_query("select * from student",$con);

echo "<table border='1' >
<tr'>
<td align=center> <b>Roll No</b></td>
<td align=center><b>Name</b></td>
<td align=center><b>Address</b></td>
<td align=center><b>Stream</b></td>
<td align=center><b>Status</b></td>";

while($data = mysqli_fetch_row($result))
{
    echo "<tr>";
    echo "<td align=center>$data[0]</td>";
    echo "<td align=center>$data[1]</td>";
    echo "<td align=center>$data[2]</td>";
    echo "<td align=center>$data[3]</td>";
    echo "<td align=center>$data[4]</td>";
    echo "</tr>";
}
echo "</table>";
?>
Copy after login

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

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template