發新話題

[分享] 《PHP》『MySQL函式庫』

《PHP》『MySQL函式庫』mysql_fetch_array --- 取得查詢後的陣列結果

mysql_fetch_array ---  取得查詢後的陣列結果

語法 : array mysql_fetch_array (int result [, int result_type])

說明 :

傳回一陣列,此陣列相當於取得列,如果沒有更多的列則傳回false。

mysql_fetch_array( )是mysql_fetch_row( )的延伸版本,除了將資料儲存在陣列的數字索引之外,它也能儲存資料在組合的索引之中,使用欄位名稱當作是索引值。

如果查詢的結果有二個以上的欄位都是相同的名稱時,則最後的欄位為有效,為了存取相同名稱的欄位,你必須為欄位取個別名。

select t1.f1 as foo t2.f1 as bar from t1, t2   

mysql_fetch_array( )的第二個參數result_type是必非需的,result_type是個常數且有以下的值 : MYSQL_ASSOC,MYSQL_NUM 及 MYSQL_BOTH(這是PHP3.0.7中新增的)

欲知詳情請參考 : mysql_fetch_row( )

Example :

<?php

    mysql_connect ($host, $user, $password);

    $result = mysql_db_query ("database","select * from table");

   while ($row = mysql_fetch_array ($result)) {

            echo $row["user_id"];

            echo $row["fullname"];

   }

   mysql_free_result ($result);

?>  


TOP

《PHP》『MySQL函式庫』mysql_fetch_field --- 取得欄位資訊

mysql_fetch_field ---  取得欄位資訊

語法 : object mysql_fetch_field (int result [, int field_offset])

說明 :

傳回一包含欄位資訊的物件。

mysql_fetch_field( )能用來為了獲得某些查詢的結果中欄位的資訊,如果沒有指定欄位的偏移量(offset),則無法以mysql_fetch_field( )獲得下一個欄位。

傳回的物件其內容有 :

name—欄位名稱

          table—欄位所在的table名稱

          max_length—欄位的最大長度

          not_null— 1表示欄位不能為空(null)的

          primary_key—1表示欄位是主要鍵(primary key)

          unique_key—1表示欄位是唯一鍵(unique key)

          multiple_key—1表示欄位非唯一鍵(non-unique key)

          numeric—1表示欄位為數值的

          blob—1表示欄位為BLOB

          type—欄位的型態

          unsigned—1表示欄位為無記號(unsigned)

          zerofill—1表示欄位被零填滿(zero-filled)

Example :

<?php

    mysql_connect ($host, $user, $password) or die ("Could not connect");

    $result = mysql_db_query ("database", "select * from table") or die ("Query failed");

      # get column metadata

   $i = 0;

   while ($i < mysql_num_fields ($result)) {

            echo "Information for column $i:<BR>\n";

            $meta = mysql_fetch_field ($result);

            if (!$meta) {

                  echo "No information available<BR>\n";

           }

           echo "<PRE>

           blob:                  $meta->blob

           max_length:      $meta->max_length

           multiple_key:    $meta->multiple_key

           name:                $meta->name

           not_null:           $meta->not_null

           numeric:           $meta->numeric

           primary_key:    $meta->primary_key

           table:                $meta->table

           type:                 $meta->type

           unique_key:     $meta->unique_key

           unsigned:         $meta->unsigned

           zerofill:            $meta->zerofill

           </PRE>";

           $i++;

        }

mysql_free_result ($result);

?>

參考 : mysql_field_seek( )

TOP

《PHP》『MySQL函式庫』mysql_fetch_lengths --- 取得各欄位最大長度

mysql_fetch_lengths ---  取得各欄位最大長度

語法 : array mysql_fetch_lengths (int result)

說明 :

將mysql_fetch_row( )取得的最後列之中各個欄位的長度放入陣列中傳回,失敗則傳回false。

mysql_fetch_lengths( )儲存由mysql_fetch_row( )、mysql_fetch_array( )、mysql_fetch_object( )所傳回的最後列之中各個欄位的長度。

參考 : mysql_fetch_row( )

TOP

《PHP》『MySQL函式庫』mysql_fetch_object --- 取得查詢後的物件結果

mysql_fetch_object ---  取得查詢後的物件結果

語法 : object mysql_fetch_object (int result [, int result_typ])

說明 :

傳回一物件,此物件相當於取得列,如果沒有更多列則傳回fales。

mysql_fetch_object( )類似於mysql_fetch_array( ),有一點不同的是此函式傳回的是物件來替代陣列,間接的意思是說你可以以欄位名稱來存取資料,且不是以它們的偏移量(offset)(數字是非法的屬性名稱)

非必需選項 result_type 是個常數且可以是下列的值 : MYSQL_ASSOC, MYSQL_NUM, MYSQL_BOTH。

此函式和mysql_fetch_array( )相同,且在速度上也和mysql_fetch_row( )差不多。

Example :

<?php

    mysql_connect ($host, $user, $password);

    $result = mysql_db_query ("database", "select * from table");

    while ($row = mysql_fetch_object ($result)) {

             echo $row->user_id; echo $row->fullname;

    }

    mysql_free_result ($result);

?>

參考 : mysql_fetch_array( )  mysql_fetch_row( )

TOP

《PHP》『MySQL函式庫』mysql_fetch_row --- 取得單列結果

mysql_fetch_row ---  取得單列結果

語法 : array mysql_fetch_row (int result)

說明 :

傳回一陣列,此陣列相當於取得列,如果沒有更多列則傳回false。

mysql_fetch_row( )從結果取得資料的一列,將列放入陣列中傳回,各個結果欄位儲存在陣列偏移量之中,偏移量的起始值為0。

隨後呼叫mysql_fetch_row( )將傳回結果中的下一列,如果沒有更多列則傳回false。

參考 : mysql_fetch_array( )  mysql_fetch_object( )  mysql_data_seek( )  mysql_fetch_lengths( )

           mysql_result( )

TOP

《PHP》『MySQL函式庫』mysql_field_flags --- 取得指定欄位的旗標

mysql_field_flags ---  取得指定欄位的旗標

語法 : string mysql_field_flags (int result, int field_offset)

說明 :

mysql_field_flags( )傳回指定欄位的欄位旗標,所傳回的旗標是個單字且每個旗標之間都以單個空白鍵來區隔,因此你可以使用explode( )將它切開來。

下列是傳回的旗標 ,如果你的MySQL版本足夠去提供它們的話 : "not_null", "primary_key","unique_key","multiple_key","blob","unsigned","zerofill","binary", "enum","auto_increment","timestamp"。

相容的mysql_fieldflages( )也何以使用。

TOP

《PHP》『MySQL函式庫』mysql_field_name --- 取得指定欄位的名稱

mysql_field_name ---  取得指定欄位的名稱

語法 : string mysql_field_name (int result, int field_index)

說明 :

mysql_field_name( )傳回指定欄位的名稱,函式的參數是結果 identifier與欄位索引,例如 : mysql_field_name($result , 2); 將會傳回結果中第二個欄位的名稱。

相容的mysql_fieldname( )也可以使用。

TOP

《PHP》『MySQL函式庫』mysql_field_len --- 取得指定欄位的長度

mysql_field_len ---  取得指定欄位的長度

語法 : int mysql_field_len (int result, int field_offset)

說明 :

mysql_field_len( )傳回指定欄位的長度,相容的mysql_fieldlen( )也可以使用。

TOP

《PHP》『MySQL函式庫』mysql_field_seek 設定結果指標到指定的欄位偏移量

mysql_field_seek ---  設定結果指標到指定的欄位偏移量

語法 : int mysql_field_seek (int result, int field_offset)

說明 :

指到指定的欄位偏移量,如果下次呼叫mysql_fetch_field( )時沒有包含欄位偏移量,則會傳回此欄位。

參考 : mysql_fetch_field( )

TOP

《PHP》『MySQL函式庫』mysql_field_table --- 取得指定欄位所在的表格名稱

mysql_field_table ---  取得指定欄位所在的表格名稱

語法 : string mysql_field_table (int result, int field_offset)

說明 :

取得欄位所在的表格名稱,相容的mysql_fieldtable( )也可以使用。

TOP

發新話題

本站所有圖文均屬網友發表,僅代表作者的觀點與本站無關,如有侵權請通知版主會盡快刪除。