mysqli有兩種數(shù)據(jù)庫連接方式:
堅守“ 做人真誠 · 做事靠譜 · 口碑至上 · 高效敬業(yè) ”的價值觀,專業(yè)網(wǎng)站建設服務10余年為成都宴會酒店設計小微創(chuàng)業(yè)公司專業(yè)提供成都企業(yè)網(wǎng)站建設營銷網(wǎng)站建設商城網(wǎng)站建設手機網(wǎng)站建設小程序網(wǎng)站建設網(wǎng)站改版,從內(nèi)容策劃、視覺設計、底層架構(gòu)、網(wǎng)頁布局、功能開發(fā)迭代于一體的高端網(wǎng)站建設服務。
1、面向過程式連接:
mysqli_connect('localhost','xxx','xxx','xxx');
mysqli_query('');
后使用mysqli_fetch_assoc方法獲取到數(shù)據(jù)。
2、面向?qū)ο笫竭B接:
$mysqli?=?new?mysqli("localhost",?"my_user",?"my_password",?"world");
$result?=?$mysqli-query('');
后使用$result-fetch_assoc()獲取數(shù)據(jù)。
至于num_rows是獲取查詢到的行數(shù)的方法。
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的每一行獲取為一個關聯(lián)數(shù)組或/和數(shù)值索引數(shù)組.
默認獲取兩種數(shù)組,result_type可以設置:
MYSQL_ASSOC:返回關聯(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)
相當于 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é)。
試編寫代碼示例如下:
?php
//連接數(shù)據(jù)庫
$db?=?new?mysqli('localhost','test','test','test');
if?($db-connect_errno)?{
printf("連接數(shù)據(jù)庫失敗:?%s\n",?$db-connect_error);
exit();
}
//對數(shù)據(jù)庫根據(jù)時間字段按降序排序,并抽取前面的40條記錄
$data?=?$db-query('select?*?from?數(shù)據(jù)表名?order?by?時間字段名?desc?limit?40');
$rows?=?$data-fetch_all(MYSQLI_ASSOC);
//隨機生成?5?條記錄數(shù)組
$rand_arr?=?array_rand($rows,?5);
//根據(jù)生成的隨機數(shù)組,輸出記錄
for($i=0;$i5;$i++)
{
echo?$rows[$rand_arr[$i]]['name'].'?|?'.$rows[$rand_arr[$i]]['description'].'?|?'.$rows[$rand_arr[$i]]['update_time'].'br/';
}
?
示例運行截圖:
$sql="select * from mytb order by time desc limit 5"
不足5條,沒關系。
凡事多嘗試。
利用自增字段,把要查詢的數(shù)據(jù)表的id設置為自增。查詢數(shù)據(jù)時,order by id desc,取第一條就是最新的。
利用時間,要查詢的數(shù)據(jù)表中增加時間字段,查詢時,order by time desc,取第一條就是最新的。
代碼如下:?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ù)庫 php數(shù)據(jù)庫查詢系統(tǒng)
網(wǎng)址分享:http://muchs.cn/article22/hjcecc.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站制作、面包屑導航、外貿(mào)建站、、App開發(fā)、標簽優(yōu)化
聲明:本網(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)