php取數(shù)據(jù)庫數(shù)據(jù),php怎么獲取數(shù)據(jù)庫中的數(shù)據(jù)

php讀取數(shù)據(jù)庫信息的幾種方法

連接到一個?url?地址為localhost?、?端口為?3306?的mysql服務器上。mysql服務器的帳號是"root",密碼是"9999"。mysql?服務器上有一個數(shù)據(jù)庫?ok?,?數(shù)據(jù)庫里有一個表?abc。表?abc?一共為兩列,列名分別是?"id"?和?"name"?,將?abc?里的所有數(shù)據(jù)讀出來。

創(chuàng)新互聯(lián)公司是一家專業(yè)提供匯川企業(yè)網(wǎng)站建設,專注與成都做網(wǎng)站、成都網(wǎng)站設計、H5技術、小程序制作等業(yè)務。10年已為匯川眾多企業(yè)、政府機構等服務。創(chuàng)新互聯(lián)專業(yè)的建站公司優(yōu)惠進行中。

??

$dbh?=?@mysql_connect("localhost:3306","root","9999");?

/*?定義變量dbh?,?mysql_connect()函數(shù)的意思是連接mysql數(shù)據(jù)庫,?"@"的意思是屏蔽報錯?*/?

if(!$dbh){die("error");}?

/*?die()函數(shù)的意思是將括號里的字串送到瀏覽器并中斷PHP程式?(Script)。括號里的參數(shù)為欲送出的字串。?*/?

@mysql_select_db("ok",?$dbh);?

/*?選擇mysql服務器里的一個數(shù)據(jù)庫,這里選的數(shù)據(jù)庫名為?ok?*/?

$q?=?"SELECT?*?FROM?abc";?

/*?定義變量q,?"SELECT?*?FROM?abc"是一個SQL語句,意思是讀取表abc中的數(shù)據(jù)?*/?

??

br?/?

!--=========?方法一?=========--?

br?/?

??

$rs?=?mysql_query($q,?$dbh);?

/*?定義變量?rs?,函數(shù)mysql_query()的意思是:送出?query?字串供?MySQL?做相關的處理或者執(zhí)行.由于php是從右往左執(zhí)行的,所以,rs的值是服務器運行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ù)一一寫出來.??

函數(shù)mysql_fetch_row()的意思是:將查詢結果$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);?

/*?關閉到mysql數(shù)據(jù)庫的連接?*/?

?

php怎么從其他的數(shù)據(jù)庫里面取數(shù)據(jù)

$con=mysql_connect('localhost','root','');//數(shù)據(jù)庫信息

mysql_select_db('shop');//數(shù)據(jù)庫名

mysql_query("set?names?utf8");//設置字符集編碼

$sql="select?goods_name,goods_number,shop_price?from?goods";//查詢語句

$res=mysql_query($sql);//執(zhí)行查詢

while($row=mysql_fetch_assoc($res)){

$rows[]=$row;//接受結果集

}

//遍歷數(shù)組

foreach($rows?as?$key=$v){

echo?$v['goods_name']."---".$v['goods_number']."---".$v['shop_price']."";

}

布局可以自己寫的。數(shù)據(jù)從foreach循環(huán)里取出。

如何正確理解PHP獲取顯示數(shù)據(jù)庫數(shù)據(jù)函數(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();

注意,上述代碼只是輸出結果集中的第一條數(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ù)的全部總結。

php如何獲取數(shù)據(jù)庫信息

代碼如下:?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";輸出記錄截圖

網(wǎng)頁題目:php取數(shù)據(jù)庫數(shù)據(jù),php怎么獲取數(shù)據(jù)庫中的數(shù)據(jù)
當前URL:http://muchs.cn/article0/hcggoo.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站制作響應式網(wǎng)站、微信公眾號商城網(wǎng)站、定制網(wǎng)站、網(wǎng)站維護

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

綿陽服務器托管