連接到一個(gè)?url?地址為localhost?、?端口為?3306?的mysql服務(wù)器上。mysql服務(wù)器的帳號(hào)是"root",密碼是"9999"。mysql?服務(wù)器上有一個(gè)數(shù)據(jù)庫(kù)?ok?,?數(shù)據(jù)庫(kù)里有一個(gè)表?abc。表?abc?一共為兩列,列名分別是?"id"?和?"name"?,將?abc?里的所有數(shù)據(jù)讀出來(lái)。
創(chuàng)新互聯(lián)公司專(zhuān)注于企業(yè)成都營(yíng)銷(xiāo)網(wǎng)站建設(shè)、網(wǎng)站重做改版、沙雅網(wǎng)站定制設(shè)計(jì)、自適應(yīng)品牌網(wǎng)站建設(shè)、H5開(kāi)發(fā)、商城網(wǎng)站建設(shè)、集團(tuán)公司官網(wǎng)建設(shè)、成都外貿(mào)網(wǎng)站建設(shè)公司、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁(yè)設(shè)計(jì)等建站業(yè)務(wù),價(jià)格優(yōu)惠性?xún)r(jià)比高,為沙雅等各大城市提供網(wǎng)站開(kāi)發(fā)制作服務(wù)。
??
$dbh?=?@mysql_connect("localhost:3306","root","9999");?
/*?定義變量dbh?,?mysql_connect()函數(shù)的意思是連接mysql數(shù)據(jù)庫(kù),?"@"的意思是屏蔽報(bào)錯(cuò)?*/?
if(!$dbh){die("error");}?
/*?die()函數(shù)的意思是將括號(hào)里的字串送到瀏覽器并中斷PHP程式?(Script)。括號(hào)里的參數(shù)為欲送出的字串。?*/?
@mysql_select_db("ok",?$dbh);?
/*?選擇mysql服務(wù)器里的一個(gè)數(shù)據(jù)庫(kù),這里選的數(shù)據(jù)庫(kù)名為?ok?*/?
$q?=?"SELECT?*?FROM?abc";?
/*?定義變量q,?"SELECT?*?FROM?abc"是一個(gè)SQL語(yǔ)句,意思是讀取表abc中的數(shù)據(jù)?*/?
??
br?/?
!--=========?方法一?=========--?
br?/?
??
$rs?=?mysql_query($q,?$dbh);?
/*?定義變量?rs?,函數(shù)mysql_query()的意思是:送出?query?字串供?MySQL?做相關(guān)的處理或者執(zhí)行.由于php是從右往左執(zhí)行的,所以,rs的值是服務(wù)器運(yùn)行mysql_query()函數(shù)后返回的值?*/?
if(!$rs){die("Valid?result!");}?
echo?"table";?
echo?"trtdID/tdtdName/td/tr";?
while($row?=?mysql_fetch_row($rs))?echo?"trtd$row[0]/tdtd$row[1]/td/tr";?
/*?定義量變(數(shù)組)row,并利用while循環(huán),把數(shù)據(jù)一一寫(xiě)出來(lái).??
函數(shù)mysql_fetch_row()的意思是:將查詢(xún)結(jié)果$rs單列拆到陣列變數(shù)中.??
$row[0]?和?$row[1]?的位置可以換*/?
echo?"/table";?
??
br?/?
!--=========?方法二?=========--?
br?/?
??
$rs?=?mysql_query($q,?$dbh);?
while($row?=?mysql_fetch_object($rs))?echo?"$row-id?$row-name?br?/";?
/*?id和name可以換位置?*/?
??
br?/?
!--=========?方法三?=========--?
br?/?
??
$rs?=?mysql_query($q,?$dbh);?
while($row?=?mysql_fetch_array($rs))?echo?"$row[id]?$row[name]?br?/";?
/*?id和name可以換位置?*/?
??
!--=========?方法三最快?=========--?
??
@mysql_close($dbh);?
/*?關(guān)閉到mysql數(shù)據(jù)庫(kù)的連接?*/?
?
試編寫(xiě)代碼如下:
?php
//從數(shù)據(jù)庫(kù)根據(jù)?id?獲取顏色
function?getColor($db,?$id)
{
if?($result?=?$db-query("SELECT?*?FROM?color?where?id='"?.?$id?.?"'"))
{
$row?=?$result-fetch_assoc();
return?$row['color'];
}
return?'#000000';
}
$mysqli?=?new?mysqli("localhost",?"test",?"test",?"room");
if?($mysqli-connect_error)?{
printf("數(shù)據(jù)庫(kù)連接錯(cuò)誤:?%s\n",?mysqli_connect_error());
exit();
}
?
table?border="1"?cellspacing="0"
tr
td?bgcolor="?php?echo?getColor($mysqli,'1')?"1/td
/tr
tr
td?bgcolor="?php?echo?getColor($mysqli,'2')?"2/td
/tr
tr
td?bgcolor="?php?echo?getColor($mysqli,'3')?"3/td
/tr
/table
?php
$mysqli-close();
?
代碼如下:?View
Code
PHP
include("conn.php");//調(diào)用數(shù)據(jù)庫(kù)連接文件
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查詢(xún)語(yǔ)句
while
($row
=
mysql_fetch_array($query)){
//使用while循環(huán)mysql_fetch_array()并將數(shù)據(jù)返回?cái)?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";輸出記錄截圖
網(wǎng)頁(yè)標(biāo)題:php怎么提取數(shù)據(jù)庫(kù)內(nèi)容 php獲取數(shù)據(jù)
網(wǎng)址分享:http://jinyejixie.com/article32/dopjppc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站制作、網(wǎng)站建設(shè)、定制網(wǎng)站、網(wǎng)站改版、小程序開(kāi)發(fā)、面包屑導(dǎo)航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)