這是PHP獲取數(shù)據(jù)庫信息的代碼 希望能給你帶來啟發(fā)
成都創(chuàng)新互聯(lián)公司2013年至今,先為雙鴨山等服務(wù)建站,雙鴨山等地企業(yè),進行企業(yè)商務(wù)咨詢服務(wù)。為雙鴨山企業(yè)網(wǎng)站制作PC+手機+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。
?php
$conn=mysql_connect("localhost","root","");
$select=mysql_select_db("books",$conn);
$query="insert into computers(name,price,publish_data) ";
$query.="values('JSP',28.00,'2008-11-1')";
$query="select * from computers";
$result=mysql_query($query);
//以下是使用mysql_result()函數(shù)來獲取到查詢結(jié)果
$num=mysql_num_rows($result);
for($rows_count=0;$rows_count$num;$rows_count++){
echo "書名:".mysql_result($result,$rows_count,"name");
echo "價格:".mysql_result($result,$rows_count,"price");
echo "出版日期:".mysql_result($result,$rows_count,"publish_data")."br";
}
//以下是使用mysql_fetch_row()函數(shù)來獲取到查詢結(jié)果
while($row=mysql_fetch_row($result))
{
echo "書號:".$row[0]."br";
echo "書名:".$row[1]."br";
echo "價格:".$row[2]."br";
echo "出版日期:".$row[3]."br";
echo "br";
}
//以下是使用mysql_fetch_array()函數(shù)來獲取到查詢結(jié)果
while($row=mysql_fetch_array($result))
{
echo "書號:".$row[0]."br";
echo "書名:".$row[1]."br";
echo "價格:".$row["price"]."br";
echo "出版日期:".$row["publish_data"]."br";
echo "br";
}
//以下是使用mysql_fetch_object()函數(shù)來獲取到查詢結(jié)果
while($row=mysql_fetch_object($result))
{
echo "書號:".$row-id."br";
echo "書名:".$row-name."br";
echo "價格:".$row-price."br";
echo "出版日期:".$row-publish_data."br";
echo "br";
}
?
試編寫代碼如下:
?php
//從數(shù)據(jù)庫根據(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ù)庫連接錯誤:?%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ù)庫連接文件
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";輸出記錄截圖