php - 使用 Jquery Ajax 从 Mysql 中检索数据

标签 php mysql ajax jquery

list.php:一个简单的ajax代码,我想只显示Mysql表的记录:

<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) {
                response = text;
            }
        });

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

<body>
    <div id="div1">
        <h2>Let jQuery AJAX Change This Text</h2>
    </div>
    <button>Get Records</button>
</body>

</html>

Records.php 是从 Mysql 中获取记录的文件。
数据库中只有两个字段:“姓名”、“地址”。

<?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);
?>
<tr>
    <td>Name: </td>
    <td>Address: </td>
</tr>
<?php
    while ($row = mysql_fetch_array($result))
    {
        echo "<tr>";
        echo "<td>$row[1]</td>";
        echo "<td>$row[2]</td>";
        echo "</tr>";
    }   
?>

此代码无效。

最佳答案

要使用 Ajax + jQuery 检索数据,您应该编写以下代码:

 <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 align="center">Manage Student Details</h3>
<table border="1" align="center">
   <tr>
       <td> <input type="button" id="display" value="Display All Data" /> </td>
   </tr>
</table>
<div id="responsecontainer" align="center">

</div>
</body>
</html>

对于mysqli连接,这样写:

<?php 
$con=mysqli_connect("localhost","root",""); 

为了显示数据库中的数据,你应该这样写:

<?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>
<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>";
?>

关于php - 使用 Jquery Ajax 从 Mysql 中检索数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16707648/

相关文章:

php - 如何使 Firefox 开发人员工具在控制台中显示 FirePHP 消息?

PHP foreach, if 语句

mysql - 如何比较两个数据库的表获取不同的mysql字段

javascript - Stripes:无法添加到通过 javascript 动态创建的表格中

php - 如何正确使用foreach()

php - 传递查询数据以查看的正确方法是什么

MySQL存储过程@语法错误

MySQL 条件插入与 insert .. select

javascript - 停止js ajax函数中的html提交按钮

jquery - WCF 4、JSONP 和 jQuery 导致解析器错误