自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

如何正確理解PHP獲取顯示數(shù)據(jù)庫數(shù)據(jù)函數(shù)

開發(fā) 后端
PHP獲取顯示數(shù)據(jù)庫數(shù)據(jù)函數(shù)包括mysql_result();mysql_fetch_row() ;mysql_fetch_array() ;mysql_fetch_assoc() ;mysql_fetch_object() 等等。

在運用PHP語言對數(shù)據(jù)庫進(jìn)行操作的時候,我們將會用到mysql_X這一函數(shù)庫。下面我們將為大家詳細(xì)介紹有關(guān)PHP獲取顯示數(shù)據(jù)庫數(shù)據(jù)函數(shù)的函數(shù)。#t#

PHP獲取顯示數(shù)據(jù)庫數(shù)據(jù)函數(shù)之 mysql_result()

mixed mysql_result(resource result_set, int row [,mixed field])
從result_set 的指定row 中獲取一個field 的數(shù)據(jù). 簡單但是效率低.

舉例:

  1. $link1 = @mysql_connect("server1", 
    "webuser", "password")   
  2. or die("Could not connect 
    to mysql server!");  
  3. @mysql_select_db("company") 
    or die("Could not select database!");  
  4. $query = "select id, name 
    from product order by name"
    ;   
  5. $result = mysql_query($query);  
  6. $id = mysql_result($result, 0, "id");  
  7. $name = mysql_result($result, 0, "name");  
  8. mysql_close();  

注意,上述代碼只是輸出結(jié)果集中的***條數(shù)據(jù)的字段值,如果要輸出所有記錄,需要循環(huán)處理.

  1. for ($i = 0; $i <= mysql_num_rows($result); $i++)  
  2. {  
  3. $id = mysql_result($result, 0, "id");  
  4. $name = mysql_result($result, 0, "name");  
  5. echo "Product: $name ($id)";  

注意,如果查詢字段名是別名,則mysql_result中就使用別名.

PHP獲取顯示數(shù)據(jù)庫數(shù)據(jù)函數(shù)之mysql_fetch_row()

array mysql_fetch_row(resource result_set)
從result_set中獲取整行,把數(shù)據(jù)放入數(shù)組中.
舉例(注意和list 的巧妙配合):
 

  1. $query = "select id, 
    name from product order by name"
    ;   
  2. $result = mysql_query($query);  
  3. while(list($id, $name) 
    = mysql_fetch_row($result)) {  
  4. echo "Product: $name ($id)";  

PHP獲取顯示數(shù)據(jù)庫數(shù)據(jù)函數(shù)之mysql_fetch_array()

array mysql_fetch_array(resource result_set [,int result_type])
mysql_fetch_row()的增強(qiáng)版.
將result_set的每一行獲取為一個關(guān)聯(lián)數(shù)組或/和數(shù)值索引數(shù)組.
默認(rèn)獲取兩種數(shù)組,result_type可以設(shè)置:
MYSQL_ASSOC:返回關(guān)聯(lián)數(shù)組,字段名=>字段值
MYSQL_NUM:返回數(shù)值索引數(shù)組.
MYSQL_BOTH:獲取兩種數(shù)組.因此每個字段可以按索引偏移引用,也可以按字段名引用.

舉例:
 

  1. $query = "select id,
     name from product order by name"
    ;  
  2. $result = mysql_query($query);  
  3. while($row = mysql_fetch_array
    ($result, MYSQL_BOTH)) {   
  4. $name = $row['name'];
  5. //或者 $name = $row[1];  
  6. $name = $row['id'];
  7. //或者 $name = $row[0];  
  8. echo "Product: $name ($id)";  

PHP獲取顯示數(shù)據(jù)庫數(shù)據(jù)函數(shù)之mysql_fetch_assoc()

array mysql_fetch_assoc(resource result_set)
相當(dāng)于 mysql_fetch_array($result, MYSQL_ASSOC)

PHP獲取顯示數(shù)據(jù)庫數(shù)據(jù)函數(shù)之mysql_fetch_object()

object mysql_fetch_object(resource result_set)
和mysql_fetch_array()功能一樣,不過返回的不是數(shù)組,而是一個對象.
舉例:

 

  1. $query = "select id, name 
    from product order by name"
    ;  
  2. $result = mysql_query($query);   
  3. while($row = mysql_fetch_object
    ($result)) {  
  4. $name = $row->name;  
  5. $name = $row->id;  
  6. echo "Product: $name ($id)";  

以上這些函數(shù)就是PHP獲取顯示數(shù)據(jù)庫數(shù)據(jù)函數(shù)的全部總結(jié)。

責(zé)任編輯:曹凱 來源: bokee.com
相關(guān)推薦

2009-12-04 17:16:41

PHP析構(gòu)函數(shù)

2010-08-05 09:53:35

2009-12-09 14:04:45

PHP include

2010-08-04 16:41:01

2010-08-11 17:24:13

DB2數(shù)據(jù)庫優(yōu)化

2013-08-06 10:40:38

大數(shù)據(jù)數(shù)據(jù)

2009-12-04 18:00:46

PHP開發(fā)MVC模型

2010-08-18 10:04:54

DB2數(shù)據(jù)庫

2010-07-05 14:58:35

SQL Server數(shù)

2010-08-05 15:40:07

DB2快照函數(shù)

2010-01-07 16:46:28

VB.NET延時函數(shù)

2009-12-03 18:07:47

PHP轉(zhuǎn)義

2009-12-07 14:53:13

PHP抽象類應(yīng)用

2010-01-18 17:29:35

VB.NET函數(shù)調(diào)用

2010-02-01 10:54:37

C++框架

2010-08-11 14:19:07

DB2 多分區(qū)數(shù)據(jù)庫備

2009-06-18 10:29:24

Hibernate I

2009-12-16 17:00:43

Ruby on Rai

2009-12-17 11:36:55

Ruby輸入輸出

2009-12-14 17:48:46

Ruby String
點贊
收藏

51CTO技術(shù)棧公眾號