用while循環(huán)例如:
發(fā)展壯大離不開(kāi)廣大客戶長(zhǎng)期以來(lái)的信賴與支持,我們將始終秉承“誠(chéng)信為本、服務(wù)至上”的服務(wù)理念,堅(jiān)持“二合一”的優(yōu)良服務(wù)模式,真誠(chéng)服務(wù)每家企業(yè),認(rèn)真做好每個(gè)細(xì)節(jié),不斷完善自我,成就企業(yè),實(shí)現(xiàn)共贏。行業(yè)涉及成都資質(zhì)代辦等,在網(wǎng)站建設(shè)公司、全網(wǎng)整合營(yíng)銷推廣、WAP手機(jī)網(wǎng)站、VI設(shè)計(jì)、軟件開(kāi)發(fā)等項(xiàng)目上具有豐富的設(shè)計(jì)經(jīng)驗(yàn)。
$res = mysql_query("select id,name,description,price,pic_url1 from ebf_products");
while($pp = mysql_fetch_array($res,MYSQL_ASSOC) or false){
$list[] = $pp;
}
代碼如下:?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查詢語(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";輸出記錄截圖
完整的代碼如下:
$con = mysql_connect('localhost(服務(wù)器地址)', '數(shù)據(jù)庫(kù)用戶名', '數(shù)據(jù)庫(kù)密碼');
//數(shù)據(jù)庫(kù)連接。
if (!$con)
{
die('Could not connect: ' . mysql_error());
}//連接失敗輸出錯(cuò)誤
mysql_select_db('數(shù)據(jù)庫(kù)名', $con);
$sql = "select Name from 表名;";
$result = mysql_query($sql,$con);
while($row= mysql_fetch_array($result)){
echo $row['Name'];
}
一般我們?yōu)榱藴p少數(shù)據(jù)庫(kù)鏈接,取數(shù)據(jù)是一次取出所有想要的數(shù)據(jù)然后做循環(huán)處理,而不是一個(gè)個(gè)循環(huán)取出
$servername?=?"localhost";
$username?=?"root";
$password?=?"root";
$dbname?=?"aaaa";
//?創(chuàng)建連接
$conn?=?new?mysqli($servername,?$username,?$password,?$dbname);
//?Check?connection
if?($conn-connect_error)?{
die("連接失敗:?"?.?$conn-connect_error);
}?
$conn-query('set?names?utf8');
$sql?=?"SELECT?name?FROM?xiao?";//這里是查詢xiao表的name列的所有數(shù)據(jù)
$result?=?$conn-query($sql);
if?($result-num_rows??0)?{
//?輸出數(shù)據(jù)
while($row?=?$result-fetch_assoc())?{
//print_r($row);
echo?"name:?"?.?$row["name"]."br";//這里是循環(huán)打印
}
}?else?{
echo?"沒(méi)有查詢到數(shù)據(jù)";
}
$conn-close();