ajax 可用来与数据库进行交互式通信,对于php来说很重要,本品阿将对其讲解。
ajax 数据库实例
下面的实例将演示网页如何通过 ajax 从数据库读取信息:
本教程使用到的 websites 表 sql 文件:websites.sql。
实例
选择一个网站: google 淘宝 菜鸟教程 微博 facebook
选择对应选项,用户信息会显示在这……
实例解释 - mysql 数据库
在上面的实例中,我们使用的数据库表如下所示:
mysql> select * from websites;+----+--------------+---------------------------+-------+---------+| id | name | url | alexa | country |+----+--------------+---------------------------+-------+---------+| 1 | google | https://www.google.cm/ | 1 | usa || 2 | 淘宝 | https://www.taobao.com/ | 13 | cn || 3 | 菜鸟教程 | http://www.runoob.com/ | 4689 | cn || 4 | 微博 | http://weibo.com/ | 20 | cn || 5 | facebook | https://www.facebook.com/ | 3 | usa |+----+--------------+---------------------------+-------+---------+5 rows in set (0.01 sec)
实例解释 - html 页面
当用户在上面的下拉列表中选择某位用户时,会执行名为 "showsite()" 的函数。该函数由 "onchange" 事件触发:
test.html 文件代码:
<!doctype html> <html> <head> <meta charset="utf-8"> <title>菜鸟教程(runoob.com)</title> <script>
function showsite(str){
if (str=="")
{
document.getelementbyid("txthint").innerhtml=""; return; }
if (window.xmlhttprequest)
{
// ie7+, firefox, chrome, opera, safari 浏览器执行代码
xmlhttp=new xmlhttprequest(); }
else
{
// ie6, ie5 浏览器执行代码
xmlhttp=new activexobject("microsoft.xmlhttp"); }
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readystate==4 && xmlhttp.status==200)
{
document.getelementbyid("txthint").innerhtml=xmlhttp.responsetext; }
}
xmlhttp.open("get","getsite_mysql.php?q="+str,true); xmlhttp.send();}
</script></head><body>
<form><select name="users" onchange="showsite(this.value)"><option value="">选择一个网站:</option><option value="1">google</option><option value="2">淘宝</option><option value="3">菜鸟教程</option><option value="4">微博</option><option value="5">facebook</option></select></form><br><div id="txthint"><b>网站信息显示在这里……</b></div>
</body></html>
showsite() 函数会执行以下步骤:
检查是否有网站被选择
创建 xmlhttprequest 对象
创建在服务器响应就绪时执行的函数
向服务器上的文件发送请求
请注意添加到 url 末端的参数(q)(包含下拉列表的内容)
php 文件
上面这段通过 javascript 调用的服务器页面是名为 "getsite_mysql.php" 的 php 文件。
"getsite_mysql.php" 中的源代码会运行一次针对 mysql 数据库的查询,然后在 html 表格中返回结果:
getsite_mysql.php 文件代码:
<?php$q = isset($_get["q"]) ? intval($_get["q"]) : '';
if(empty($q)) {
echo '请选择一个网站'; exit;}
$con = mysqli_connect('localhost','root','123456');if (!$con){
die('could not connect: ' . mysqli_error($con));}// 选择数据库mysqli_select_db($con,"test");// 设置编码,防止中文乱码mysqli_set_charset($con, "utf8");
$sql="select * from websites where id = '".$q."'";
$result = mysqli_query($con,$sql);
echo "<table border='1'>
<tr>
<th>id</th>
<th>网站名</th>
<th>网站 url</th>
<th>alexa 排名</th>
<th>国家</th>
</tr>";
while($row = mysqli_fetch_array($result)){
echo "<tr>"; echo "<td>" . $row['id'] . "</td>"; echo "<td>" . $row['name'] . "</td>"; echo "<td>" . $row['url'] . "</td>"; echo "<td>" . $row['alexa'] . "</td>"; echo "<td>" . $row['country'] . "</td>"; echo "</tr>";}echo "</table>";
mysqli_close($con);?>
解释:当查询从 javascript 发送到 php 文件时,将发生:
php 打开一个到 mysql 数据库的连接
找到选中的用户
创建 html 表格,填充数据,并发送回 txthint 占位符
本篇对php - ajax 与 mysql之间的交互做了详细的讲解,更多的学习资料清关注即可观看。
相关推荐:
关于php - ajax 与 php之间的联系
关于php simple xml的相关知识
关于php xml dom的相关知识点
以上就是关于php - ajax 与 mysql之间的交互的详细内容。