PHP Conference Japan 2024

mysql_field_type

(PHP 4, PHP 5)

mysql_field_type取得結果中指定欄位的型別

警告

此擴充功能在 PHP 5.5.0 中已被棄用,並在 PHP 7.0.0 中移除。應改用 MySQLiPDO_MySQL 擴充功能。另請參閱 MySQL:選擇 API 指南。此函式的替代方案包括

描述

mysql_field_type(resource $result, int $field_offset): string

mysql_field_type() 類似於 mysql_field_name() 函式。引數相同,但會改為傳回欄位型別。

參數

result

正在評估的結果 resource。此結果來自對 mysql_query() 的呼叫。

field_offset

數字欄位偏移量。field_offset0 開始。如果 field_offset 不存在,也會發出 E_WARNING 等級的錯誤。

傳回值

傳回的欄位型別將為 "int""real""string""blob" 以及 » MySQL 文件中詳述的其他型別。

範例

範例 1 mysql_field_type() 範例

<?php
mysql_connect
("localhost", "mysql_username", "mysql_password");
mysql_select_db("mysql");
$result = mysql_query("SELECT * FROM func");
$fields = mysql_num_fields($result);
$rows = mysql_num_rows($result);
$table = mysql_field_table($result, 0);
echo
"您的 '" . $table . "' 資料表有 " . $fields . " 個欄位和 " . $rows . " 筆記錄\n";
echo
"資料表有下列欄位:\n";
for (
$i=0; $i < $fields; $i++) {
$type = mysql_field_type($result, $i);
$name = mysql_field_name($result, $i);
$len = mysql_field_len($result, $i);
$flags = mysql_field_flags($result, $i);
echo
$type . " " . $name . " " . $len . " " . $flags . "\n";
}
mysql_free_result($result);
mysql_close();
?>

上面的範例會輸出類似下面的內容

Your 'func' table has 4 fields and 1 record(s)
The table has the following fields:
string name 64 not_null primary_key binary
int ret 1 not_null
string dl 128 not_null
string type 9 not_null enum

注意

注意:

為了向後相容性,可以使用以下已棄用的別名:mysql_fieldtype()

參見

新增註解

使用者提供的註解 6 則註解

3
swalif_mesa at hotmail dot com
21 年前
嗯,對於先前的評論,請注意 SHOW FIELDS 是 SHOW COLUMNS 的別名。如果您需要搜尋 mysql 文件,這非常有用。無論如何,這是連結...
https://mysql.dev.org.tw/doc/en/SHOW_DATABASE_INFO.html
2
fusionstream at gmail dot com
18 年前
krang at krang dot org dot uk
KRANG 所說的濃縮版本。

若要取得 MYSQL 欄位型別的資訊,請使用此程式碼

<?
$result = mysql_query("SHOW FIELDS FROM db_name");

$i = 1;
while ($row = mysql_fetch_array($result)) { // 一次處理一個欄位
echo "欄位 $i: ";
print_r($row) // 顯示有關可以透過 "$row" 陣列存取之欄位的所有資訊。
$i++;
}
?>

將顯示類似下面的內容
欄位 1: Array ( [Field] => SN [Type] => mediumint(8) unsigned [Null] => [Key] => PRI [Default] => [Extra] => auto_increment ) , SN, mediumint(8) unsigned
欄位 2: Array ( [Field] => ENTITY_ID [Type] => varchar(20) [Null] => [Key] => [Default] => [Extra] => ) , ENTITY_ID, varchar(20)
-1
c dot futterlieb at bluewin dot ch
17 年前
針對以下備註的補充說明
如果您使用 "SHOW FIELDS" 語法,請務必在 "FROM" 參數中加入表格名稱,而不是資料庫名稱。

<?php
$result
= mysql_query("SHOW FIELDS FROM table");
?>

也可以這樣

<?php
$result
= mysql_query("SHOW FIELDS FROM table.db");
?>

或者這樣

<?php
$result
= mysql_query("DESCRIBE table");
?>
https://mysqldev.dev.org.tw/doc/refman/5.1/en/describe.html
-2
mariob at menta dot net
21 年前
在 4.3.4 版本中,回傳的資料型態為:

STRING, VAR_STRING: 字串
TINY, SHORT, LONG, LONGLONG, INT24: 整數
FLOAT, DOUBLE, DECIMAL: 實數
TIMESTAMP: 時間戳記
YEAR: 年份
DATE: 日期
TIME: 時間
DATETIME: 日期時間
TINY_BLOB, MEDIUM_BLOB, LONG_BLOB, BLOB: blob
NULL: 空值
其他任何類型:未知
-3
fred at dinkler dot com
17 年前
在 MySQL 4.1.x 中,四種 TEXT 類型 (TINYTEXT、TEXT、MEDIUMTEXT 和 LONGTEXT) 會回傳 'blob' 作為欄位類型,而不是 'string'。
-5
krang at krang dot org dot uk
22 年前
欄位類型回傳的是 PHP 如何分類欄位中的資料,而不是資料在資料庫中的儲存方式;請使用以下範例來檢索關於欄位的 MySQL 資訊....

$USERNAME = '';
$PASSWORD = '';

$DATABASE = '';
$TABLE_NAME = '';

mysql_connect('localhost', $USERNAME, $PASSWORD)
or die ("Could not connect");

$result = mysql_query("SHOW FIELDS FROM $DATABASE.$TABLE_NAME");

$i = 0;

while ($row = mysql_fetch_array($result)) {
echo $row['Field'] . ' ' . $row['Type'];
}
To Top