非常教程

PHP参考手册

PHP 实例 AJAX 与 MySQL

PHP 实例 AJAX 与 MySQL

PHP - AJAX 与 MySQL


AJAX 可用来与数据库进行交互式通信。


AJAX 数据库实例

下面的实例将演示网页如何通过 AJAX 从数据库读取信息:

本教程使用到的 Websites 表 SQL 文件:websites.sql。

实例


选择对应选项,用户信息会显示在这……



实例解释 - 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.verydoc.net/    | 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>非常教程(verydoc.net)</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 文件时,将发生:

  1. PHP 打开一个到 MySQL 数据库的连接
  2. 找到选中的用户
  3. 创建 HTML 表格,填充数据,并发送回 "txtHint" 占位符
PHP 实例 AJAX 与 MySQL

PHP目录

1.PHP Error 和 Logging 函数
2.PHP 5 Directory 函数
3.PHP 5 Date/Time 函数
4.PHP 5 Calendar 函数
5.PHP 5 Array 函数
6.PHP 5 SimpleXML 函数
7.PHP 5 MySQLi 函数
8.PHP Misc. 函数
9.PHP 5 Math 函数
10.PHP Mail 函数
11.PHP Libxml 函数
12.PHP HTTP 函数
13.PHP FTP 函数
14.PHP Filter 函数
15.PHP 5 Filesystem 函数
16.PHP 简介
17.PHP 教程
18.PHP Zip File 函数
19.PHP XML 函数
20.PHP 5 String 函数
21.PHP 语法
22.PHP 安装
23.PHP If…Else 语句
24.PHP 运算符
25.PHP 字符串变量
26.PHP 变量
27.PHP 函数
28.PHP For 循环
29.PHP While 循环
30.PHP 数组排序
31.PHP 数组
32.PHP Switch 语句
33.PHP include 和 require
34.PHP date() 函数
35.PHP 安全 E-mail
36.PHP 邮件
37.PHP Session
38.PHP Cookie
39.PHP 文件
40.PHP 异常处理
41.PHP 错误处理
42.PHP MySQL 读取数据
43.PHP MySQL 插入数据
44.PHP MySQL 创建数据库
45.PHP MySQL 简介
46.PHP – AJAX 与 PHP
47.PHP XML SimpleXML
48.PHP XML DOM
49.PHP XML Expat 解析器
50.PHP 数据库 ODBC
51.PHP MySQL Delete
52.PHP MySQL Update
53.PHP MySQL Order By 关键词
54.PHP MySQL Where 子句
55.PHP 实例 AJAX 投票
56.PHP 实例 AJAX RSS 阅读器
57.PHP 实例 AJAX 实时搜索
58.PHP 实例 AJAX 与 XML
59.PHP 实例 AJAX 与 MySQL
60.PHP array() 函数
61.PHP 5 Timezones
62.PHP array_fill() 函数
63.PHP array_diff_ukey() 函数
64.PHP array_diff_uassoc() 函数
65.PHP array_diff_key() 函数
66.PHP array_diff_assoc() 函数
67.PHP array_diff() 函数
68.PHP array_count_values() 函数
69.PHP array_combine() 函数
70.PHP array_chunk() 函数
71.PHP array_change_key_case() 函数
72.PHP array_key_exists() 函数
73.PHP array_intersect_ukey() 函数
74.PHP array_intersect_uassoc() 函数
75.PHP array_intersect_key() 函数
76.PHP array_intersect_assoc() 函数
77.PHP array_intersect() 函数
78.PHP array_flip() 函数
79.PHP array_filter() 函数
80.PHP array_fill_keys() 函数
81.PHP array_map() 函数
82.PHP array_keys() 函数
83.PHP array_reduce() 函数
84.PHP array_rand() 函数
85.PHP array_push() 函数
86.PHP array_product() 函数
87.PHP array_pop() 函数
88.PHP array_pad() 函数
89.PHP array_multisort() 函数
90.PHP array_merge_recursive() 函数
91.PHP array_merge() 函数
92.PHP array_uintersect() 函数
93.PHP array_udiff_uassoc() 函数
94.PHP array_udiff_assoc() 函数
95.PHP array_udiff() 函数
96.PHP array_sum() 函数
97.PHP array_splice() 函数
98.PHP array_slice() 函数
99.PHP array_shift() 函数
100.PHP array_search() 函数