大概的基本流程如下:

創(chuàng)新互聯(lián)一直通過網(wǎng)站建設(shè)和網(wǎng)站營銷幫助企業(yè)獲得更多客戶資源。 以"深度挖掘,量身打造,注重實效"的一站式服務(wù),以網(wǎng)站建設(shè)、網(wǎng)站制作、移動互聯(lián)產(chǎn)品、網(wǎng)絡(luò)營銷推廣服務(wù)為核心業(yè)務(wù)。十余年網(wǎng)站制作的經(jīng)驗,使用新網(wǎng)站建設(shè)技術(shù),全新開發(fā)出的標(biāo)準(zhǔn)網(wǎng)站,不但價格便宜而且實用、靈活,特別適合中小公司網(wǎng)站制作。網(wǎng)站管理系統(tǒng)簡單易用,維護方便,您可以完全操作網(wǎng)站資料,是中小公司快速網(wǎng)站建設(shè)的選擇。
連接數(shù)據(jù)庫,再加一個判斷。
選擇數(shù)據(jù)庫
讀取表
輸出表中數(shù)據(jù)
下面是代碼:
?php
$con = mysql_connect("localhost","root","abc123");
/* localhost 是服務(wù)器 root 是用戶名?abc123 是密碼*/?
if (!$con)
{
die("數(shù)據(jù)庫服務(wù)器連接失敗");
}
/*?這就是一個邏輯非判斷,如果錯誤就輸出括號里的字符串 */?
@mysql_select_db("a",?$con);?
/*?選擇mysql服務(wù)器里的一個數(shù)據(jù)庫,假設(shè)你的數(shù)據(jù)庫名為?a*/
$sql?=?"SELECT?*?FROM qq";?
/* 定義變量sql,?"SELECT?*?FROM?qq" 是SQL指令,表示選取表qq中的數(shù)據(jù)?*/
$result = mysql_query($sql); //執(zhí)行SQL語句,獲得結(jié)果集
/*下面就是選擇性的輸出打印了,由于不清楚你的具體情況給你個表格打印吧*/
//打印表格?
echo "table border=1";?
while( $row = mysql_fetch_array($result) )
/*逐行獲取結(jié)果集中的記錄,得到數(shù)組row?*/
{ ?
/*數(shù)組row的下標(biāo)對應(yīng)著數(shù)據(jù)庫中的字段值?*/
$id = $row['id'];?
$name = $row['name'];?
$sex = $row['sex'];?
echo "tr";?
echo "td$id/td";?
echo "td$name/td";?
echo "td$sex/td";?
echo "/tr";?
}?
echo "table /";
?
如果你的switch是表頭,就定義這個表頭字段,然后輸出。
1、PHP獲取顯示數(shù)據(jù)庫數(shù)據(jù)函數(shù)之 mysql_result()
mixed mysql_result(resource result_set, int row [,mixed field])
從result_set 的指定row 中獲取一個field 的數(shù)據(jù). 簡單但是效率低.
舉例:
$link1?=?@mysql_connect("server1",?
"webuser",?"password")?
or?die("Could?not?connect?
to?mysql?server!");
@mysql_select_db("company")?
or?die("Could?not?select?database!");
$query?=?"select?id,?name?
from?product?order?by?name";?
$result?=?mysql_query($query);
$id?=?mysql_result($result,?0,?"id");
$name?=?mysql_result($result,?0,?"name");
mysql_close();
注意,上述代碼只是輸出結(jié)果集中的第一條數(shù)據(jù)的字段值,如果要輸出所有記錄,需要循環(huán)處理.
for?($i?=?0;?$i?=?mysql_num_rows($result);?$i++)
{
$id?=?mysql_result($result,?0,?"id");
$name?=?mysql_result($result,?0,?"name");
echo?"Product:?$name?($id)";
}
注意,如果查詢字段名是別名,則mysql_result中就使用別名.
2、PHP獲取顯示數(shù)據(jù)庫數(shù)據(jù)函數(shù)之mysql_fetch_row()
array mysql_fetch_row(resource result_set)
從result_set中獲取整行,把數(shù)據(jù)放入數(shù)組中.
舉例(注意和list 的巧妙配合):
$query?=?"select?id,?
name?from?product?order?by?name";?
$result?=?mysql_query($query);
while(list($id,?$name)?
=?mysql_fetch_row($result))?{
echo?"Product:?$name?($id)";
}
3、PHP獲取顯示數(shù)據(jù)庫數(shù)據(jù)函數(shù)之mysql_fetch_array()
array mysql_fetch_array(resource result_set [,int result_type])
mysql_fetch_row()的增強版.
將result_set的每一行獲取為一個關(guān)聯(lián)數(shù)組或/和數(shù)值索引數(shù)組.
默認獲取兩種數(shù)組,result_type可以設(shè)置:
MYSQL_ASSOC:返回關(guān)聯(lián)數(shù)組,字段名=字段值?
MYSQL_NUM:返回數(shù)值索引數(shù)組.
MYSQL_BOTH:獲取兩種數(shù)組.因此每個字段可以按索引偏移引用,也可以按字段名引用.
舉例:
$query?=?"select?id,
name?from?product?order?by?name";
$result?=?mysql_query($query);
while($row?=?mysql_fetch_array
($result,?MYSQL_BOTH))?{?
$name?=?$row['name'];
//或者?$name?=?$row[1];
$name?=?$row['id'];
//或者?$name?=?$row[0];
echo?"Product:?$name?($id)";
}
4、PHP獲取顯示數(shù)據(jù)庫數(shù)據(jù)函數(shù)之mysql_fetch_assoc()
array mysql_fetch_assoc(resource result_set)
相當(dāng)于 mysql_fetch_array($result, MYSQL_ASSOC)
5、PHP獲取顯示數(shù)據(jù)庫數(shù)據(jù)函數(shù)之mysql_fetch_object()
object mysql_fetch_object(resource result_set)?
和mysql_fetch_array()功能一樣,不過返回的不是數(shù)組,而是一個對象.
舉例:
$query?=?"select?id,?name?
from?product?order?by?name";
$result?=?mysql_query($query);?
while($row?=?mysql_fetch_object
($result))?{
$name?=?$row-name;
$name?=?$row-id;
echo?"Product:?$name?($id)";
}
以上這些函數(shù)就是PHP獲取顯示數(shù)據(jù)庫數(shù)據(jù)函數(shù)的全部總結(jié)。
代碼如下:?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";輸出記錄截圖
要用javascript調(diào)用php獲取數(shù)據(jù)庫接口,是一個很常見的前后端交互操作
通過javascript發(fā)送http請求php的API接口,php連接數(shù)據(jù)庫并查詢結(jié)果,最后返回出來
這樣javascript就能獲取到數(shù)據(jù)庫的數(shù)據(jù)
//頁面語句
var?userid?=?getElementById('#username');
var?passwd?=?getElementById('#password');
$.ajax({
url:'后臺處理地址',
dataType:'JSON',
type:'POST',
data:'username='+userid+'passwd='+passwd,
error:?function(){
//post失敗
}
success:?function(data)?{//post成功
if?(data.s=='ok')?{
//成功信息,處理語句
}else?{
//失敗信息,處理語句
}
}
});
//后臺語句
if?(count($volist)??0)?{//有數(shù)據(jù)
......//處理語句
$data?=?array('s'='ok','html'=$html,'page'='span?class="page"'.$show.'/span');
echo?json_encode($data);
}else?{//無數(shù)據(jù)
$html?=?"tr?class='tr'td?class='tc'?colspan='11'暫無數(shù)據(jù),等待添加~!/td/tr";
$data?=?array('s'='no','html'=$html);
echo?json_encode($data);
}
大概是這樣吧
獲取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ù)組名,當(dāng)獲取不到時
mysql_fetch_array()或者用mysql_fetch_row()返回
FALSE。
名稱欄目:PHP實時獲取數(shù)據(jù)庫,php獲取數(shù)據(jù)庫數(shù)據(jù)
網(wǎng)頁鏈接:http://chinadenli.net/article42/dsgicec.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供服務(wù)器托管、搜索引擎優(yōu)化、移動網(wǎng)站建設(shè)、網(wǎng)站改版、手機網(wǎng)站建設(shè)、響應(yīng)式網(wǎng)站
聲明:本網(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)