PHP
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

mssql_fetch_object> <mssql_fetch_batch
Last updated: Fri, 03 Oct 2008

view this page in

mssql_fetch_field

(PHP 4, PHP 5, PECL odbtp:1.1.1-1.1.4)

mssql_fetch_fieldフィールド情報を取得する

説明

object mssql_fetch_field ( resource $result [, int $field_offset ] )

mssql_fetch_field() は、あるクエリー結果のフィールドに関して情報を得るために使用します。

パラメータ

result

処理対象となる結果リソース。これは mssql_query() のコールによって取得します。

field_offset

フィールドオフセット。フィールドオフセットが指定されない場合、 mssql_fetch_field() によりまだ取得されていない次のフィールドが取得されます。 field_offset は 0 から始まります。

返り値

フィールド情報を含むオブジェクトを返します。

オブジェクトのプロパティは次のようになります。

  • name - カラム名。カラムが関数の結果である場合、このプロパティは computed#N に設定されます。ただし、#N はシリアル番号です。
  • column_source - そのカラムが取得されたテーブル
  • max_length - カラムの最大長
  • numeric - そのカラムが数字である場合に 1
  • type - カラムの型


mssql_fetch_object> <mssql_fetch_batch
Last updated: Fri, 03 Oct 2008
 
add a note add a note User Contributed Notes
mssql_fetch_field
not a mail address
20-Aug-2008 12:22
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.
schattenfeld at gmail dot com
02-Aug-2007 04:43
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;
huszti_dot_roland_at_freemail_dot_com
11-May-2006 02:51
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.
php_rindern_de
25-Oct-2004 10:36
commenting Reynard Hilman:

for me it looks like the colstat field value of 1 in syscolumns table indicates an Identity Column.
Reynard Hilman
23-Oct-2003 04:40
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
bmaddy_at_class_dot_umn_dot_edu
30-Apr-2003 04:41
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
alonf at spiralsolutions dot com
28-Nov-2002 11:47
As kubalaa at bigfoot dot com note mssql_fetch_field->column_source return field name instead table name also with MSSQL2000 connection. Be adwised!!!
pong at taft dot org
16-Mar-2002 02:07
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.
skipsey at hotmail dot com
08-Feb-2001 04:58
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;

    }
}
?>
kubalaa at bigfoot dot com
05-Apr-2000 12:58
Using this function with MSSQL 7, $returned->column_source is the column name, not the table name as it should be.

mssql_fetch_object> <mssql_fetch_batch
Last updated: Fri, 03 Oct 2008
 
 
show source | credits | sitemap | contact | advertising | mirror sites