2024 年 PHP Conference Japan

odbc_field_name

(PHP 4, PHP 5, PHP 7, PHP 8)

odbc_field_name取得欄位名稱

說明

odbc_field_name(Odbc\Result $statement, int $field): string|false

取得指定結果物件中,指定欄位編號的欄位名稱。

參數

statement

ODBC 結果物件。

field

欄位編號。欄位編號從 1 開始。

傳回值

傳回欄位名稱字串,若發生錯誤則傳回 false

更新日誌

版本 說明
8.4.0 statement 現在需要 Odbc\Result 實例;先前需要的是 資源
新增筆記

使用者貢獻的筆記 11 則筆記

3
hayes029 at bama dot ua dot edu
20 年前
為了尋找一個可以簡單地傳回結果識別碼中欄位名稱陣列的函式,我唯一能找到的就是 odbc_field_name 函式。因此,對於任何其他正在尋找此類函式的人,以下是我寫的(非常簡單的)函式

function odbc_field_names ($result) {
for ($i=1; $i <= odbc_num_fields($result); $i++) $return_array[$i-1] = odbc_field_name($result, $i);
return $return_array;
}

我知道這很簡單,但我認為它可能會有幫助。
0
anuga at anuga dot se
15 年前
我一直在嘗試這個,讓它盡可能簡單快速。

<?php

/* 開始連線 */
if(!$odbc['connection'] = odbc_connect('DNS','USER','PASS'))
{
exit(
"連線失敗<br />\n");
}
else
{
echo(
"已連線<br />\n");
}

/* 選擇表格 */
$odbc['table'] = "table";

/* 將欄位名稱擷取到陣列中 */
if($result = odbc_exec($odbc['connection'],"select * from $odbc['table'];"))
{
for(
$i = 1;$i <= odbc_num_fields($result);$i++)
{
$odbc['rows']['fields'][$i] = odbc_field_name($result,$i);
}
unset(
$i);
odbc_free_result($result);
}
else
{
exit(
"SQL 查詢錯誤");
}

/* 關閉連線 */
if(odbc_close($odbc['connection']))
{
odbc_close($odbc['connection']);
}

/* 印出陣列 */
if(!empty($odbc['rows']))
{
print_r($odbc['rows']);
}

?>
0
sica at wnet dot com dot br
16 年前
嗨 hayes029,你的想法很有幫助。
這裡提供一個適用於選擇表單欄位的改編版本。
<?php
$consulta
= "select * from schema.table";
$resposta = odbc_exec($con, $consulta);
for(
$i=1; $i <= odbc_num_fields($resposta); $i++)
echo
"<option value=".odbc_field_name($resposta, $i).">".odbc_field_name($resposta, $i)."</option>";
?>
0
marco
16 年前
我們在使用 cakephp dbo_odbc.php 類別 (版本 1.1.18.5850) 時遇到了 31 個字元的限制

最後,它呼叫了 odbc_fetch_row 導致了這個問題(我追蹤發現可能是由於 php 實作 ODBCv2 模型所致,該模型硬編碼了 4 位元組,32 位元的檔案名稱長度,而 ODBCv3 似乎有單獨的規格定義該值,僅供參考)

我們從另一位使用者(也遇到 dbo_mssql.php 類別的類似問題)那裡學到了如何解決這個問題。

我們透過繼承解決了這個問題,重新定義了方法(在我們的應用程式中局部地)以便
- 在執行查詢之前,建立查詢欄位的「映射」(以陣列形式)
- 以非關聯方式執行查詢
- 在返回結果後,應用先前建立的「映射」還原真實的欄位名稱

另一種方法可能是使用 COM,例如
new COM("ADODB.Connection")
這種方法不會出現 31 個字元的問題,但速度可能會較慢,並且受限於 Windows 平台。
0
NoEgzit
17 年前
如果您真的需要超過 31 個字元,您可以這樣做

使用像 notepad++ 之類的編輯器開啟 php5.x.x/ext/odbc/php_odbc_includes.h

將 32 改成更大的值
typedef struct odbc_result_value {
char name[32];
char *value;
SDWORD vallen;
SDWORD coltype;
} odbc_result_value;

然後重新編譯 PHP。

我將其改為 char name[64],因為我的欄位名稱像「0214_1_VD_Type d'exploitation Type d'opération (Niveau 1)」這樣[這個愚蠢的名稱不是我取的,它來自 Eccairs 專案]
並使用這裡提供的方法重建 PHP https://php.dev.org.tw/manual/en/install.windows.building.php 以及 http://elizabethmariesmith.com/2006/11/09/
compiling-php52-on-windows-with-net-toolchain-is-it-even-possible/

使用 Visual C++ Express Edition。
0
jezzghost
18 年前
請注意,這有一個已知的限制,它會將返回的欄位名稱長度截斷為 31 個字元,且不會發出警告。
0
andrea dot galli at acotel dot com
21 年前
範例:函式欄位名稱。

$Link_ID = odbc_connect("DSN", "user", "pass");

$query = "SELECT * FROM products";

$Query_ID = odbc_exec($Link_ID, $query);

while($field = $field_name($Query_ID ))
{
echo("Field: $field<br />\n");
}

---------------------

function field_name($PrQuery_ID)
{
if($Column < odbc_num_fields($PrQuery_ID))
{
$Column += 1;
$FieldName = odbc_field_name($PrQuery_ID, $Column);

return $FieldName;
}
else
{
return 0;
}
}
0
aleckzandr at yahoo dot com
21 年前
嗯,我接觸 PHP 四個小時了,多虧了「我的前輩們」(gold163、curt 等人),我完成了以下工作。我學習任何網路腳本語言時,首先嘗試的就是從資料來源建立動態表格。(gold - 前一篇貼文 - 你不需要做的一件事是為欄位值建立一個陣列。)乾杯!Alex

<html>
<head>
<title>PHP 資料庫範例</title>
</head>
<style type="text/css">
<!--
body {font: 10pt/12pt Tahoma, Verdana, Helvetica, sans-serif; color: indigo; margin: .25in .5in }
table {color:Navy; background-color:AntiqueWhite; border-color:Maroon; border-style:Solid; border-width: 2px; }
th {color: blue; font-weight: bold; }
td {font-size: smaller; }
.mytable {color:Maroon; background-color:White; border-color:Navy; border-style:Solid; border-width: 1px; }
th.mytable {background-color:#C0C0C0; }
//-->
</style>
<body>

<p><?php echo date("j F, Y"); ?></p>
<?php

$db
= odbc_connect("eSell22MDB","","");
$result = odbc_exec($db, "select ProductID, ProductName, Description1 from Products");

// cool function - returns table
odbc_result_all($result, "border=\"1\" class=\"def\"");

$result = odbc_exec($db, "select * from Products") or die("Select failed");

$myUtil = new Utilities();

$myUtil->standard_table($result,"mytable");

class
Utilities {

function
standard_table($result,$class="")
{
// To format your table if you want to use cascading style sheets
if ($class == "")
{
$css_table = " border=\"1\"";
$css_tr = "";
$css_th = "";
$css_td = "";
}
else
{
$css_table = " class=\"$class\"";
$css_tr = " class=\"$class\"";
$css_th = " class=\"$class\"";
$css_td = " class=\"$class\"";
}

// Create field names for table header row
$i = 0;
$fieldCount = odbc_num_fields($result);
echo
" <table$css_table>\n";
echo
" <tr$css_tr>\n";

while (
$i < $fieldCount)
{
$i++;
$fieldName = odbc_field_name($result, $i);
echo
" <th$css_th>$fieldName</th>\n";
}
echo
" </tr>\n";

# Create table data rows for query result
while (odbc_fetch_row($result))
{
$i = 0;
echo
" <tr$css_tr>\n";
while (
$i < $fieldCount)
{
$i++;
$fieldData = trim(odbc_result($result, $i));
if (
$fieldData == "")
echo
" <td$css_td>&nbsp;</td>\n";
else
echo
" <td$css_td>$fieldData</td>\n";
}
echo
" </tr>\n";
}
echo
" </table>";
}
}
// class Utilities

?>

</body>
</html>
0
gold163 at lisco dot com
21 年前
利用你的程式碼,再進一步,我可以透過呼叫 include 檔案中的函式,用一行程式碼建立一個標準表格——額外的好處是,我可以選擇性地提供樣式表類別名稱作為參數——進一步簡化表格的格式化。

你在這些論壇中發現了什麼樣的協同效應?

$Conn = odbc_connect('dsn','user','pass');
$query = "SELECT * FROM yourtable";
$result = odbc_exec($Conn, $query) or die('選取失敗!');

standard_table($result);

函式 standard_table($result,$class='')
{
# 如果要使用層疊樣式表來格式化表格
if ($class == '')
{
$css_table = ' border=1';
$css_tr = '';
$css_th = '';
$css_td = '';
}
else
{
$css_table = ' class=\"$class\"';
$css_tr = ' class=\"$class\"';
$css_th = ' class=\"$class\"';
$css_td = ' class=\"$class\"';
}

# 建立表格標題列的欄位名稱
$i = 0;
$fCount = odbc_num_fields($result);
echo "<table$css_table><tr>";
while ($i < $fCount)
{
$i++;
$fName = odbc_field_name($result, $i);
echo "<th>$fName</th>";
}
echo "</tr>";

# 建立查詢結果的表格資料列
$i = 0;
$fCount = odbc_num_fields($result);
while (odbc_fetch_row($result))
{
echo "<tr>";
while ($i < $fCount)
{
$i++;
$fName = odbc_field_name($result, $i);
$job[$fName] = odbc_result($result, $i);
echo "<td>$job[$fName]</td>";
}
echo "</tr>";
$i = 0;
}
echo "</table>";
}
0
匿名
23 年前
哇,我終於有東西可以貢獻了。
如果你跟我一樣,一直在尋找一種方法,用適當的名稱和值來命名和填入變數,而不是命名每個變數並使用 odbc_result($result, 1), odbc_result($result, 2) 等等...那麼這個小迴圈就是為你準備的!把它用作函式可能會更好,但我相信你自己也能做到,對吧?

<?php
$query
= "SELECT * FROM TableName";
$result = odbc_exec($conn, $query) or die('選取失敗!');
$i = 0;
$fCount = odbc_num_fields($result);

while (
odbc_fetch_row($result)) {
while (
$i < $fCount) {
$i++;
$fName = odbc_field_name($result, $i);
$job[$fName] = odbc_result($result, $i);
}
$i=0;
}
?>

這段程式碼應該很容易理解,之後你可以隨時使用表格中的欄位名稱來存取變數。目前我正在用它們的實際值來存取它們,並使用這個簡單的方法來避免在程式碼的頂部輸入所有變數名稱。祝你使用愉快。

Jason/ArtHacker.com
-1
curt at digmo dot com
23 年前
我將 Jason 的程式碼轉換成一個函式,大致模仿 mysql_fetch_array 函式。我不是程式設計師,而且我接觸 PHP 還不到一個星期,所以我認為一定有比我想出的方法更有效率的做法。


函式 odbc_fetch_array($rownum, $res)
{

$i = 0;
$fCount = odbc_num_fields($res);
odbc_fetch_row($res, $rownum);
while ($i < $fCount)
{
$i++;
$fName = odbc_field_name($res, $i);

/* $fName = odbc_field_name($res, $i); 取得結果集 $res 中第 $i 個欄位的名稱,並將其賦值給變數 $fName。*/
$myrow[$fName] = odbc_result($res, $i);

/* 將結果集 $res 中第 $i 個欄位的值賦值給關聯陣列 $myrow,其中鍵名為對應的欄位名稱 $fName。*/
}
$i=0;

/* 將變數 $i 初始化為 0。*/
return $myrow;

/* 返回關聯陣列 $myrow。*/
}

To Top