要用javascript調(diào)用php獲取數(shù)據(jù)庫接口,是一個很常見的前后端交互操作

創(chuàng)新互聯(lián)是專業(yè)的鷹手營子網(wǎng)站建設(shè)公司,鷹手營子接單;提供成都做網(wǎng)站、成都網(wǎng)站制作,網(wǎng)頁設(shè)計,網(wǎng)站設(shè)計,建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進行鷹手營子網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團隊,希望更多企業(yè)前來合作!
通過javascript發(fā)送http請求php的API接口,php連接數(shù)據(jù)庫并查詢結(jié)果,最后返回出來
這樣javascript就能獲取到數(shù)據(jù)庫的數(shù)據(jù)
從查詢結(jié)果取值,需要遍歷結(jié)果集!示例如下:
$rs?=?mysql_query("select?*?from?www_liu?where?xx='$xx'?and?yy='$yy'");
echo?"查詢信息如下:br/";
while($row?=?mysql_fetch_array($rs))
{
echo?$row['字段2']?.?"====="?.?$row['字段三'];
echo?"br?/";
}
//關(guān)閉數(shù)據(jù)庫連接
//mysql_close();
1、用navicat新建一個數(shù)據(jù)庫database1。
2、在database1數(shù)據(jù)庫中新建一個表table2。
3、在table2中添加新的數(shù)據(jù),新建一個名稱為mysql_query的數(shù)據(jù)庫。
4、在頁面中用mysql_connect 函數(shù)與數(shù)據(jù)庫建立連接。
5、用mysql_select_db函數(shù)選擇要查詢的數(shù)據(jù)庫。
6、添加一個查詢 table2表的查詢語句“$sql=select * from table2“。
7、將查詢語句$sql添加到查詢數(shù)據(jù)庫函數(shù)mysql_query中,返回值賦值給變量query。
8、最后將mysql_query。php文件在瀏覽器中打開,查看查詢到數(shù)據(jù)庫中的內(nèi)容的結(jié)果。
獲取ppq數(shù)據(jù)庫的所有表名的代碼:
?php
$server='localhost';
$user='root';
$pass='12345';
$dbname='ppq';
$conn=mysql_connect($server,$user,$pass);
if(!$conn)
die("數(shù)據(jù)庫系統(tǒng)連接失敗!");
$result=mysql_list_tables($dbname);
if(!$result)
die("數(shù)據(jù)庫連接失敗!");
while($row=mysql_fetch_row($result))
{
echo
$row[0]."
";
}
mysql_free_result($result);
?
mysql_list_tables
(PHP
3,
PHP
4
,
PHP
5)
mysql_list_tables
--
列出
MySQL
數(shù)據(jù)庫中的表
說明
resource
mysql_list_tables
(
string
database
[,
resource
link_identifier])
mysql_list_tables()
接受一個數(shù)據(jù)庫名并返回和
mysql_query()
函數(shù)很相似的一個結(jié)果指針。用
mysql_fetch_array()或者用mysql_fetch_row()來獲得一個數(shù)組,數(shù)組的第0列就是數(shù)組名,當獲取不到時
mysql_fetch_array()或者用mysql_fetch_row()返回
FALSE。
將$rs=mysql_fetch_object($result)改為
$rs=mysql_fetch_assoc($result)或者$rs=mysql_fetch_array($result)就可以實現(xiàn)。
具體原因:
mysql有四種方法獲取結(jié)果的當前行數(shù)據(jù),分別是fetch_row(),fetch_assco(),fetch_array(),fetch_object()區(qū)別分別是返回的數(shù)據(jù)的類型不同,依次分別是索引數(shù)組(即索引是數(shù)字0,1,2等),關(guān)聯(lián)數(shù)組(即索引是具體的真實的鍵等),兼前二者,對象。
代碼如下:?View
Code
PHP
include("conn.php");//調(diào)用數(shù)據(jù)庫連接文件
echo
"table
width=572
height=56
border=0
cellspacing=1
";
//創(chuàng)建html表格
echo
"tr
bgcolor=#9999FF";
echo
"th
width=33
scope=colid/th";
echo
"th
width=100
scope=coluser_name/th
";
echo
"th
width=100
scope=coluser_pass/th
";
echo
"th
width=100
scope=colstaus/th";
echo
"th
width=100
scope=colinsert_time/th";
echo
"/tr";
$SQL
=
"select
*
from
user_info";
$query
=
mysql_query($SQL);
//SQL查詢語句
while
($row
=
mysql_fetch_array($query)){
//使用while循環(huán)mysql_fetch_array()并將數(shù)據(jù)返回數(shù)組
echo
"tr
onmouseout=this.style.backgroundColor=''
onMouseOver=this.style.backgroundColor='#99CC33'
bgcolor=#CCCCCC";
echo
"td$row[0]/td";
//輸出數(shù)組中數(shù)據(jù)
echo
"td$row[1]/td";
echo
"td$row[2]/td";
echo
"td$row[3]/td";
echo
"td$row[4]/td";
echo
"/tr";
}
echo
"/table";輸出記錄截圖
分享標題:如何獲取php數(shù)據(jù)庫結(jié)果,php怎么獲取數(shù)據(jù)庫中的數(shù)據(jù)
網(wǎng)頁鏈接:http://chinadenli.net/article27/dssppjj.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供域名注冊、靜態(tài)網(wǎng)站、網(wǎng)站排名、虛擬主機、外貿(mào)建站、電子商務(wù)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)