《PHP-AJAX 與 MySQL》要點:
本文介紹了PHP-AJAX 與 MySQL,希望對您有用。如果有疑問,可以聯(lián)系我們。
AJAX 可用來與數(shù)據(jù)庫進行交互式通信.
AJAX 數(shù)據(jù)庫實例
下面的實例將演示網(wǎng)頁如何通過 AJAX 從數(shù)據(jù)庫讀取信息:
本教程使用到的 Websites 表 SQL 文件:websites.sql.
實例
選擇對應選項,用戶信息會顯示在這……
實例解釋 - MySQL 數(shù)據(jù)庫
在上面的實例中,我們使用的數(shù)據(jù)庫表如下所示:
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 頁面
當用戶在上面的下拉列表中選擇某位用戶時,會執(zhí)行名為 "showSite()" 的函數(shù).該函數(shù)由 "onchange" 變亂觸發(fā):
test.html 文件代碼:
<!DOCTYPEhtml><html><head><metacharset="utf-8"><title>菜鳥教程(runoob.com)</title><script>
functionshowSite(str){if(str==""){document.getElementById("txtHint").innerHTML=""; return; }if(window.XMLHttpRequest){// IE7+, Firefox, Chrome, Opera, Safari 瀏覽器執(zhí)行代碼xmlhttp=newXMLHttpRequest(); }else{// IE6, IE5 瀏覽器執(zhí)行代碼xmlhttp=newActiveXObject("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><selectname="users"onchange="showSite(this.value)"><optionvalue="">選擇一個網(wǎng)站:</option><optionvalue="1">Google</option><optionvalue="2">淘寶</option><optionvalue="3">菜鳥教程</option><optionvalue="4">微博</option><optionvalue="5">Facebook</option></select></form><br><divid="txtHint"><b>網(wǎng)站信息顯示在這里……</b></div></body></html>
showSite() 函數(shù)會執(zhí)行以下步調(diào):
檢查是否有網(wǎng)站被選擇
創(chuàng)建 XMLHttpRequest 對象
創(chuàng)建在服務器響應就緒時執(zhí)行的函數(shù)
向服務器上的文件發(fā)送哀求
請注意添加到 URL 末端的參數(shù)(q)(包括下拉列表的內(nèi)容)
PHP 文件
上面這段通過 JavaScript 調(diào)用的服務器頁面是名為 "getsite_mysql.php" 的 PHP 文件.
"getsite_mysql.php" 中的源代碼會運行一次針對 MySQL 數(shù)據(jù)庫的查詢,然后在 HTML 表格中返回成果:
getsite_mysql.php 文件代碼:
<?php$q = isset($_GET["q"]) ? intval($_GET["q"]) : ''; if(empty($q)){echo'請選擇一個網(wǎng)站'; exit;}$con = mysqli_connect('localhost','root','123456');if(!$con){die('Could not connect: ' . mysqli_error($con));}// 選擇數(shù)據(jù)庫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>網(wǎng)站名</th><th>網(wǎng)站 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 發(fā)送到 PHP 文件時,將產(chǎn)生:
PHP 打開一個到 MySQL 數(shù)據(jù)庫的連接
找到選中的用戶
創(chuàng)建 HTML 表格,填湊數(shù)據(jù),并發(fā)送回 "txtHint" 占位符
歡迎參與《PHP-AJAX 與 MySQL》討論,分享您的想法,維易PHP學院為您提供專業(yè)教程。
轉(zhuǎn)載請注明本頁網(wǎng)址:
http://www.snjht.com/jiaocheng/7893.html