PHP Conference Japan 2024

mysqli_result::fetch_field_direct

mysqli_fetch_field_direct

(PHP 5, PHP 7, PHP 8)

mysqli_result::fetch_field_direct -- mysqli_fetch_field_direct取得單一欄位的元資料

描述

物件導向風格

public mysqli_result::fetch_field_direct(int $index): object|false

程序風格

mysqli_fetch_field_direct(mysqli_result $result, int $index): object|false

從指定的結果集傳回一個包含欄位定義資訊的物件。

參數

result

僅限程序風格:由 mysqli_query()mysqli_store_result()mysqli_use_result()mysqli_stmt_get_result() 傳回的 mysqli_result 物件。

index

欄位編號。此值必須在 0欄位數量 - 1 的範圍內。

傳回值

如果沒有指定 index 的欄位資訊,則傳回包含欄位定義資訊的物件,或 false

物件屬性
屬性 描述
name 資料行的名稱
orgname 如果指定別名,則為原始資料行名稱
table 此欄位所屬的表格名稱(如果不是計算得出的)
orgtable 如果指定別名,則為原始表格名稱
def 未使用。始終為空字串
db 資料庫的名稱
catalog 未使用。始終為 "def"
max_length 結果集中欄位的最大寬度。從 PHP 8.1 開始,此值始終為 0
length 欄位的寬度,以位元組為單位。對於字串資料行,長度值會因連線字元集而異。例如,如果字元集為 latin1,這是一個單一位元組字元集,則 SELECT 'abc' 查詢的長度值為 3。如果字元集為 utf8mb4,這是一個多位元組字元集,其中字元最多佔用 4 個位元組,則長度值為 12。
charsetnr 欄位的字元集編號。
flags 表示欄位位元旗標的整數。
type 此欄位使用的資料類型
decimals 數值欄位的小數位數,以及時間欄位的小數秒精確度。

範例

範例 #1 物件導向風格

<?php
$mysqli
= new mysqli("localhost", "my_user", "my_password", "world");

/* 檢查連線 */
if (mysqli_connect_errno()) {
printf("連線失敗:%s\n", mysqli_connect_error());
exit();
}

$query = "SELECT Name, SurfaceArea from Country ORDER BY Name LIMIT 5";

if (
$result = $mysqli->query($query)) {

/* 取得 'SurfaceArea' 資料行的欄位資訊 */
$finfo = $result->fetch_field_direct(1);

printf("名稱:%s\n", $finfo->name);
printf("表格:%s\n", $finfo->table);
printf("最大長度:%d\n", $finfo->max_length);
printf("旗標:%d\n", $finfo->flags);
printf("類型:%d\n", $finfo->type);

$result->close();
}

/* 關閉連線 */
$mysqli->close();
?>

範例 #2 程序風格

<?php
$link
= mysqli_connect("localhost", "my_user", "my_password", "world");

/* 檢查連線 */
if (mysqli_connect_errno()) {
printf("連線失敗:%s\n", mysqli_connect_error());
exit();
}

$query = "SELECT Name, SurfaceArea from Country ORDER BY Name LIMIT 5";

if (
$result = mysqli_query($link, $query)) {

/* 取得 'SurfaceArea' 資料行的欄位資訊 */
$finfo = mysqli_fetch_field_direct($result, 1);

printf("名稱:%s\n", $finfo->name);
printf("表格:%s\n", $finfo->table);
printf("最大長度:%d\n", $finfo->max_length);
printf("旗標:%d\n", $finfo->flags);
printf("類型:%d\n", $finfo->type);

mysqli_free_result($result);
}

/* 關閉連線 */
mysqli_close($link);
?>

以上範例會輸出

Name:     SurfaceArea
Table:    Country
max. Len: 10
Flags:    32769
Type:     4

參見

新增註解

使用者貢獻的註解 12 則註解

daniel at summit cove dot com
15 年前
這是一個更大的資料類型列表。我透過建立所有我能建立的類型並呼叫 fetch_fields() 取得此列表。

<?php
$mysql_data_type_hash
= array(
1=>'tinyint',
2=>'smallint',
3=>'int',
4=>'float',
5=>'double',
7=>'timestamp',
8=>'bigint',
9=>'mediumint',
10=>'date',
11=>'time',
12=>'datetime',
13=>'year',
16=>'bit',
//252 目前對應到所有文字和 blob 類型 (MySQL 5.0.51a)
253=>'varchar',
254=>'char',
246=>'decimal'
);
?>
ben dot NOSPAM at NOSPAM dot seraphire dot com
11 年前
這可能很明顯,但欄位類型的常數已經在 PHP 中定義,可以在以下的文件中找到: https://php.dev.org.tw/manual/en/mysqli.constants.php
cjs at ashdowntech dot com
16 年前
根據
http://www.redferni.uklinux.net/mysql/MySQL-Protocol.html

資料類型值為

DECIMAL 0 ENUM 247
TINY 1 SET 248
SHORT 2 TINY_BLOB 249
LONG 3 MEDIUM_BLOB 250
FLOAT 4 LONG_BLOB 251
DOUBLE 5 BLOB 252
NULL 6 VAR_STRING 253
TIMESTAMP 7 STRING 254
LONGLONG 8 GEOMETRY 255
INT24 9
DATE 10
TIME 11
DATETIME 12
YEAR 13
NEWDATE 14

請注意,這未經測試,且不包含 deluxmozart 註記的所有值
deluxmozart 註記的所有值
cjs at ashdowntech dot com
16 年前
根據
dev.mysql.com/sources/doxygen/mysql-5.1/mysql__com_8h-source.html
旗標位元為

NOT_NULL_FLAG 1 /* 欄位不能為 NULL */
PRI_KEY_FLAG 2 /* 欄位是主鍵的一部分 */
UNIQUE_KEY_FLAG 4 /* 欄位是唯一鍵的一部分 */
MULTIPLE_KEY_FLAG 8 /* 欄位是索引的一部分 */
BLOB_FLAG 16 /* 欄位是 blob */
UNSIGNED_FLAG 32 /* 欄位是無號數 */
ZEROFILL_FLAG 64 /* 欄位是零填充 */
BINARY_FLAG 128 /* 欄位是二進位 */
ENUM_FLAG 256 /* 欄位是 enum */
AUTO_INCREMENT_FLAG 512 /* 欄位是自動遞增欄位 */
TIMESTAMP_FLAG 1024 /* 欄位是時間戳記 */
andre at koethur dot de
10 年前
以下是兩種將 'type' 和 'flags' 屬性轉換為文字以進行除錯的方法。它們都使用預定義的 MYSQLI_ 常數來產生文字。

<?php

public static function h_type2txt($type_id)
{
static
$types;

if (!isset(
$types))
{
$types = array();
$constants = get_defined_constants(true);
foreach (
$constants['mysqli'] as $c => $n) if (preg_match('/^MYSQLI_TYPE_(.*)/', $c, $m)) $types[$n] = $m[1];
}

return
array_key_exists($type_id, $types)? $types[$type_id] : NULL;
}

public static function
h_flags2txt($flags_num)
{
static
$flags;

if (!isset(
$flags))
{
$flags = array();
$constants = get_defined_constants(true);
foreach (
$constants['mysqli'] as $c => $n) if (preg_match('/MYSQLI_(.*)_FLAG$/', $c, $m)) if (!array_key_exists($n, $flags)) $flags[$n] = $m[1];
}

$result = array();
foreach (
$flags as $n => $t) if ($flags_num & $n) $result[] = $t;
return
implode(' ', $result);
}

?>
gmx dot net at Hoffmann dot P
10 年前
警告!
此函數不僅比預期的更耗用記憶體,而且記憶體消耗也取決於結果集的大小。因此,如果您真的只想取得您的 field_names,您可能需要附加「LIMIT 1」或使用 mysqli->unbuffered_query() 來避免記憶體膨脹。
shoresofnowhere at gmail dot com
15 年前
請注意 ->def 屬性永遠不會填入正確的預設欄位值,因此它完全無用。

這不是因為 php 中的錯誤造成的 (所以不要填寫錯誤報告),而是根據設計造成的,因為 MySQL C API 呼叫不會填入此值,除非您呼叫 mysql_list_fields() 函數,而 Php 沒有呼叫。

請參閱此處以供參考。
https://mysqldev.dev.org.tw/doc/refman/5.0/en/c-api-datatypes.html

此外,請注意,如果您使用包含子查詢的查詢,即使您正在查看的欄位是主表格的主鍵自動遞增鍵,主鍵/自動遞增旗標也不會傳遞。

SELECT * from (SELECT id from areas) AS subareas

您會發現 id 欄位的主鍵和 autoinc 旗標已關閉,即使 id 是 areas 表格的主鍵自動遞增鍵。

我認為這也是根據設計,因為假設我們正在使用子查詢,那麼主鍵/autoinc 東西可能根本沒有意義,因為在結果集中,我們可以從許多不同的表格組成欄位。

希望這很有用,再見!
anatoliy at ukhvanovy dot name
9 年前
您可以在這裡找到所有可用的資料類型
https://php.dev.org.tw/manual/ru/mysqli.constants.php
(在您的瀏覽器中搜尋 "MYSQLI_TYPE_")
匿名
4 年前
如果有任何人想知道,JSON 類型為 245。
bl at example dot com
14 年前
請注意
"SELECT <timestamp-field>, ..."
會傳回類型為 7 (時間戳記) 的欄位,但內容類似 "2010-07-14 14:35:08"。重點是它是一個字串。

"SELECT <timestamp-field> + 0, ..."
會傳回類型為 5 (double),但雖然是數字,但不是自 epoch 以來的秒數,而是 MySQL "YYYYMMDDHHMMSS" 格式的數字,在此範例中
20100714143508

(PHP 5.2.12)
deluxmozart at yahoo dot de
16 年前
這裡有一些關於資料型態的數字。我搜尋了一下,但沒有找到一個列出數字資料型態的清單。

所以首先我可以提供這些

3 - 整數 (Int)
10 - 日期 (Date)
246 - 小數 (Decimal)
252 - 文字 (text)
253 - 可變字元 (VarChar)
254 - 布林 (Boolean)
gcdreak at example dot com
15 年前
我寫了一個簡單的類別來取得欄位的資訊。
試試看!

<?php
class MysqlFieldsInfo implements Iterator
{

private
$result;
private
$position;
private
$row;


function
__construct($result){
$this->result = $result;
$this->position = 0;
$this->rewind(); // W $results 內部指標可能會被移動,所以我們回到開頭
}

public function
current(){
return
$this->row;
}

public function
next(){
$this->position++;
$this->row = $this->result->fetch_field();
}

public function
valid(){
return (boolean)
$this->row;
}

public function
key(){
return
$this->position;
}

public function
rewind(){
$this->position = 0;
$this->result->field_seek(0);
$this->next();
}

// 這個函式會在表格中顯示資料
public function export(){

echo
'<table id="db_table_info">';
echo
'<tr>
<th>名稱</th>
<th>原始名稱</th>
<th>表格</th>
<th>原始表格</th>
<th>預設值</th>
<th>最大長度</th>
<th>長度</th>
<th>字元集編號</th>
<th>旗標</th>
<th>型別</th>
<th>小數位數</th>
</tr>'
;
while(
$this->valid()){
echo
'<tr>';
printf("\n\t<td>%s</td>\n", $this->current()->name);
printf("\t<td>%s</td>\n", $this->current()->orgname);
printf("\t<td>%s</td>\n", $this->current()->orgtable);
printf("\t<td>%s</td>\n", $this->current()->def);
printf("\t<td>%s</td>\n", $this->current()->max_length);
printf("\t<td>%s</td>\n", $this->current()->length);
printf("\t<td>%s</td>\n", $this->current()->charsetnr);
printf("\t<td>%s</td>\n", $this->current()->flags);
printf("\t<td>%s</td>\n", $this->current()->type);
printf("\t<td>%s</td>\n", $this->current()->decimals);

echo
'</tr>';

$this->next();
}

echo
'</table>';
}
}
?>
To Top