快速掌握PHP獲取字段數(shù)據(jù)類型技巧
對(duì)于一個(gè)初學(xué)者來(lái)說(shuō),他們?cè)趯W(xué)習(xí)的過(guò)程中肯定要學(xué)到靈活的運(yùn)用各種PHP中與數(shù)據(jù)庫(kù)相關(guān)的函數(shù)來(lái)實(shí)現(xiàn)自己的需求。現(xiàn)在,就讓我們一起來(lái)看看有關(guān)PHP獲取字段數(shù)據(jù)類型的方法介紹。#t#
mysql_field_type()函數(shù)可獲得字段的數(shù)據(jù)類型,該函數(shù)的語(yǔ)法格式如下。
string mysql_field_type ( resource $result, int $field_offset )
mysql_field_type()函數(shù)類似于函數(shù)mysql_field_name(),不過(guò)mysql_field_type()函數(shù)返回的是字段的數(shù)據(jù)類型。它的參數(shù)的描述如下。
l result:mysql_query()函數(shù)執(zhí)行后返回的結(jié)果集。
l field_offset:字段的偏移量,起始值為零。
使用mysql_field_type()函數(shù)的示例代碼如下:
代碼23-12 光盤\codes\第23章\23.3\mysql_field_type.php
- < ?php
- $connection=mysql_connect("localhost",
"root","root") or die("連接服務(wù)器失敗");- mysql_select_db("sunyang",$connection)
or die("選擇數(shù)據(jù)庫(kù)失敗");- $query="select * from employee";
- $result=mysql_query($query) or die
("查詢數(shù)據(jù)失敗");- //執(zhí)行查詢
- echo mysql_field_type($result,0);
- //***個(gè)字段的數(shù)據(jù)類型
- echo "< br>";
- echo mysql_field_type($result,1);
- //第二個(gè)字段的數(shù)據(jù)類型
- echo "< br>";
- echo mysql_field_type($result,2);
- //第三個(gè)字段的數(shù)據(jù)類型
- mysql_free_result($result);
- mysql_close();
- ?>
上面代碼執(zhí)行后將把表employee中的前3個(gè)字段的數(shù)據(jù)類型輸出。