使用jQuery阿贾克斯检索MySQL数据 [英] Using Jquery Ajax to retrieve data from Mysql
问题描述
我学习,但还是有些混乱,所以我把我的code在这里。
list.php的对于我想要显示的MySQL表
只记录了简单的Ajax code
I have learn but still some confusion so i put my code here.
list.php For simple ajax code that i want to display only records of Mysql table
<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记录 在数据库只有两个字段:姓名,地址
Records.php This file to fetch records from Mysql In Database Only Two fields: 'Name','Address'
<?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>";
}
?>
这code不是working.Suggestion对我怎么解决我的问题。
谢谢
That code is not working.Suggestion for me how i solve my problem.
Thank You
推荐答案
有关检索使用Ajax + jQuery的数据,你必须写以下code:
For retrieving data using Ajax+jQuery, you must write the following code:
<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 load_page.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>
有关MySQL连接,这样写:
For mysql connection, write this:
<?php
$con=mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
?>
有关显示从数据库中的数据,你必须这样写:
For displaying the data from database, you must write this :
<?php
include("connection.php");
mysql_select_db("samples",$con);
$result=mysql_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 = mysql_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>";
?>
这篇关于使用jQuery阿贾克斯检索MySQL数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!