该页面用 PHP 编写,并使用 MySQL 数据库。
其中的代码执行针对数据库的 SQL 查询,并以 HTML 表格返回结果:
<?php
$q=$_GET["q"]
$con = mysql_connect('localhost', 'peter', 'abc123')
if (!$con)
{
die('Could not connect: ' . mysql_error())
}
mysql_select_db("ajax_demo", $con)
$sql="SELECT * FROM user WHERE id = '".$q."'"
$result = mysql_query($sql)
echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
<th>Hometown</th>
<th>Job</th>
</tr>"
while($row = mysql_fetch_array($result))
{
echo "<tr>"
echo "<td>" . $row['FirstName'] . "</td>"
echo "<td>" . $row['LastName'] . "</td>"
echo "<td>" . $row['Age'] . "</td>"
echo "<td>" . $row['Hometown'] . "</td>"
echo "<td>" . $row['Job'] . "</td>"
echo "</tr>"
}
echo "</table>"
mysql_close($con)
?>
例子解释:
当查询从 JavaScript 被发送到这个 PHP 页面,会发生:
PHP 打开到达 MySQL 服务器的连接
找到拥有指定姓名的 "user"
创建表格,插入数据,然后将其发送到 "txtHint" 占位符
var json = [{"id":1,"age":5},
{"id":2,"age":4},
{"id":3,"age":3},
{"id":4,"age":2},
{"id":5,"age":1}
]
for(var i=0,len=json.lengthi<leni++){
$('eml').append(<p>json[i].id</p>)
}
要先引入 jquery , json是假设接口返回的数据。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)