伍佰目录 短网址
  当前位置:海洋目录网 » 站长资讯 » 站长资讯 » 文章详细 订阅RssFeed

PHP - Manual: mssql_fetch_field

来源:网络转载 浏览:52次 时间:2023-07-12
mssql_fetch_object » « mssql_fetch_batch PHP 手册 函数参考 数据库扩展 针对各数据库系统对应的扩展 Mssql Mssql 函数

mssql_fetch_field

(PHP 4, PHP 5, PECL odbtp >= 1.1.1)

mssql_fetch_field — Get field information

Warning

This function was REMOVED in PHP 7.0.0.

Alternatives to this function include:

PDOStatement::getColumnMeta() sqlsrv_field_metadata() The various odbc_field_* functions in the unified ODBC driver.

说明

mssql_fetch_field ( resource $result [, int $field_offset = -1 ] ) : object

mssql_fetch_field() can be used in order to obtain information about fields in a certain query result.

参数

result

The result resource that is being evaluated. This result comes from a call to mssql_query().

field_offset

The numerical field offset. If the field offset is not specified, the next field that was not yet retrieved by this function is retrieved. The field_offset starts at 0.

返回值

Returns an object containing field information.

The properties of the object are:

name - column name. if the column is a result of a function, this property is set to computed#N, where #N is a serial number. column_source - the table from which the column was taken max_length - maximum length of the column numeric - 1 if the column is numeric type - the column type.

范例

Example #1 mssql_fetch_field() example

<?php
// Connect to MSSQL and select the database
mssql_connect('MANGO\SQLEXPRESS', 'sa', 'phpfi');
mssql_select_db('php');

// Send a select query to MSSQL
$query = mssql_query('SELECT * FROM [php].[dbo].[persons]');

// Construct table
echo '<h3>Table structure for \'persons\'</h3>';
echo '<table border="1">';

// Table header
echo '<thead>';
echo '<tr>';
echo '<td>Field name</td>';
echo '<td>Data type</td>';
echo '<td>Max length</td>';
echo '</tr>';
echo '</thead>';

// Dump all fields
echo '<tbody>';

for ($i = 0; $i < mssql_num_fields($query); ++$i) {
    // Fetch the field information
    $field = mssql_fetch_field($query, $i);

    // Print the row
    echo '<tr>';
    echo '<td>' . $field->name . '</td>';
    echo '<td>' . strtoupper($field->type) . '</td>';
    echo '<td>' . $field->max_length . '</td>';
    echo '</tr>';
}

echo '</tbody>';
echo '</table>';

// Free the query result
mssql_free_result($query);
?>

参见

mssql_field_seek() - Seeks to the specified field offset
add a note

User Contributed Notes 10 notes

up down 0 schattenfeld at gmail dot com11 years ago If you want to describe table like DESCRIBE in MySQL you can use this:
$sql = <<<SQL
SELECT column_name,data_type,column_default,is_nullable
FROM
  information_schema.tables AS t
  JOIN
  information_schema.columns AS c ON
    t.table_catalog=c.table_catalog AND
    t.table_schema=c.table_schema AND
    t.table_name=c.table_name
WHERE
  t.table_name='TABLE-NAME'
SQL;
up down 0 huszti_dot_roland_at_freemail_dot_com13 years ago For really detailed table information, use syscolumns, like this:

SELECT c.name, c.prec, c.scale, t.name type
  FROM syscolumns c, systypes t, sysobjects o
  WHERE o.name = 'yourtablename' AND o.id = c.id AND c.xtype = t.xtype

For other properties see the MS SQL online help. Search for 'syscolumns'.

Or an another solution:

sp_columns @table_name = 'yourtablename', @column_name = 'thecolumnname'
//no "select ..." !!!!

This gives info about only the specified column.
up down 0 php_rindern_de14 years ago commenting Reynard Hilman:

for me it looks like the colstat field value of 1 in syscolumns table indicates an Identity Column.
up down 0 Reynard Hilman15 years ago If you want to describe table structure (like mysql 'desc table' command), sending this query might help:
<?
$sql = "SELECT c.name, c.isnullable, c.length, c.colstat, t.name type
  FROM syscolumns c, systypes t, sysobjects o
  WHERE o.name = '$table' AND o.id = c.id AND c.xtype = t.xtype";
?>
I suspect the colstat field in syscolumns table indicates primary key when its value is 1
up down 0 bmaddy_at_class_dot_umn_dot_edu16 years ago Be aware that this function will only return the first 30 characters of the name of the column.  If the actual column name is longer, it will be truncated.  This is at least true with the following setup:
PHP 4.3.1
MSSQL 8.00.760

Have a good day everyone!
Brian
up down 0 alonf at spiralsolutions dot com16 years ago As kubalaa at bigfoot dot com note mssql_fetch_field->column_source return field name instead table name also with MSSQL2000 connection. Be adwised!!! up down -1 pong at taft dot org17 years ago When you mssql_fetch_field(int result), you need to do loop to get the name of each field.  Something like:

while($fld = mssql_fetch_field($rs)){
        echo $fld->name . "<br>";        
    }

I am wondering why we cannot refer it by a field number.
up down -1 kubalaa at bigfoot dot com19 years ago Using this function with MSSQL 7, $returned->column_source is the column name, not the table name as it should be. up down -2 not a mail address10 years ago kubalaa at bigfoot dot com first reported the problem that column_source contains the FIELD name instead of the source TABLE name 04-Apr-2000 03:58. The bug has been reported as well.

For those who wonder if it was fixed over the past 8 years: No, the bug still is the same, no workaround available.
up down -2 skipsey at hotmail dot com18 years ago It seems fairly hard to get a list of the tables from your database using MSSQL but this seems to do the trick. This is set to get only the User Tables and ignores the sytem tables.

<?php
mssql_connect("server","","") or die ("help me!");
mssql_select_db("") or die ("Noooo!");

$result = mssql_query ("sp_tables");
$fields = mssql_num_fields ($result);
$rows   = mssql_num_rows ($result);

for ($f=0; $f<$rows; $f++) {   
    $CHKTYPE=GetField($result,"TABLE_TYPE",$f);
   
    if($CHKTYPE=='TABLE'){

        //$name = mssql_fetch_field($result, 2);
        $field=GetField($result,"TABLE_NAME",$f)."<br>";
        echo $field;

    }
}
?>
add a note

官方地址:https://www.php.net/manual/en/function.mssql-fetch-field.php

  推荐站点

  • At-lib分类目录At-lib分类目录

    At-lib网站分类目录汇集全国所有高质量网站,是中国权威的中文网站分类目录,给站长提供免费网址目录提交收录和推荐最新最全的优秀网站大全是名站导航之家

    www.at-lib.cn
  • 中国链接目录中国链接目录

    中国链接目录简称链接目录,是收录优秀网站和淘宝网店的网站分类目录,为您提供优质的网址导航服务,也是网店进行收录推广,站长免费推广网站、加快百度收录、增加友情链接和网站外链的平台。

    www.cnlink.org
  • 35目录网35目录网

    35目录免费收录各类优秀网站,全力打造互动式网站目录,提供网站分类目录检索,关键字搜索功能。欢迎您向35目录推荐、提交优秀网站。

    www.35mulu.com
  • 就要爱网站目录就要爱网站目录

    就要爱网站目录,按主题和类别列出网站。所有提交的网站都经过人工审查,确保质量和无垃圾邮件的结果。

    www.912219.com
  • 伍佰目录伍佰目录

    伍佰网站目录免费收录各类优秀网站,全力打造互动式网站目录,提供网站分类目录检索,关键字搜索功能。欢迎您向伍佰目录推荐、提交优秀网站。

    www.wbwb.net