mssql_fetch_array

(PHP 3, PHP 4, PHP 5)

mssql_fetch_array --  Fetch a result row as an associative array, a numeric array, or both

Description

array mssql_fetch_array ( resource result [, int result_type] )

Returns: An array that corresponds to the fetched row, or FALSE if there are no more rows.

mssql_fetch_array() is an extended version of mssql_fetch_row(). In addition to storing the data in the numeric indices of the result array, it also stores the data in associative indices, using the field names as keys.

An important thing to note is that using mssql_fetch_array() is NOT significantly slower than using mssql_fetch_row(), while it provides a significant added value.

注: 本函数返回的字段名是区分大小写的。

注: 本函数对 PHP NULL 值设定 NULL 字段。

For further details, also see mssql_fetch_row().


add a note add a note User Contributed Notes
admin at talente dot ro
19-Oct-2005 11:36
There is any option like skip or go next ! I am using a while clause inside:
  While ($row=mysql_fetch_array($result))
   while ($row["Brand_name"]=$gr){
     ....
So I am printing different TD from classes and when I finish cycling the fields I want to "skip" to next record !
gillis dot phpmanual at no-spam-for-megillis dot fi
06-Jul-2005 04:19
Allthough probably obvious to people who have used the functions available to other databases it during writing is not present in this entry that result type can be entered as MSSQL_ASSOC or MSSQL_NUM, and the default value when nothing entered being MSSQL_BOTH

//Gillis Danielsen
29-Dec-2004 04:58
The last comment is false. For BIT fields NULL values are returned as '0', but also the 0 value is returned as '0'. We didn't find a way to distinguish NULL values from 0 values in BIT fields.
29-Dec-2004 12:16
For fields of the type BIT, the NULLs are distinguishable from 0 values by the next statement:

<?php
if ($bitValue === '0') {
   echo
'This is a SQL-server NULL value for a bit field';
} else {
   echo
'Value is not null';
}
?>

In the fields of type BIT this: ($strValue === null) won't work.

PHP 4.2.3 as ISAPI on Windows 2000 Server/SQL Server 2000
archwisp at gmail dot com
26-Oct-2004 10:44
In response to my last post:

After further testing, it appears as though the null column bug only affects windows installations. On my linux installation using freetds-0.61.2, the function behaves properly. It must exist either in the php_mssql.dll or the Microsoft connection libraries. And since I use this functionality through ASP as well (which uses the Microsoft connection libraries), it would lead me to believe that it's an issue within the php_mssql.dll.
archwisp at gmail dot com
26-Oct-2004 05:28
[Editor's Note: NULLs *are* distinguishable from 0.  See the manual section on comparison operators. ]

microsoft sql 2000 server
php running on windows 2000

Using the $row = mssql_fetch_array($result)

A null value will return 0, this is a problem if you are using 0 and some identifier for a value.  you should convert all nulls to something like -1 or "NO VALUE"

******

After extensive testing, the above statements both seem to be true. You *can* distinguish between 0 and null values in PHP; however, the mssql_fetch_array function does not set null colums to the php NULL value.

if ($row['Null_Column'] === 0) { print('pass'); }

A null column passes the above test. Seems to be a bug in the function.
Derek Ethier
25-Feb-2004 07:06
The array_walk function is also useful for stripping the whitespace returned in an mssql_query.

function modify_field(&$array) {
   $array = trim($array);
}

$query = "select * from dbo.table where value = '0'";
$result = mssql_query($query) or die;

while ($row = mssql_fetch_array($result)) {
   // This will call the above function.
   array_walk($row, 'modify_field');
   array_push($eventresults, $row);
}

An added benefit is that you can expand the modify_field function to handled unexpected returned column data.
hackajar matt yahoo trot com
24-Jan-2004 07:11
from php-dev mailing list article -
#26012 [Bgs]: mssql_fetch_array

"Previously mssql data was always rtrimed, however that's wrong because it modifies the original data. This caused a problem for people who needed those trailing space. Ultimately, the job of database extension is to fetch the data as is (like other db extensions already do) and not to mangle it in any way."

moving from <4.3.4 to a higher version you will have this now:

$query = "Select dumb_spaces from dbo.table where weak_sause = 'true'";
$result = mssql_query($query) or die("Spicy Sause! Query = $query");

while($line = mssql_fetch_array($result, MSSQL_ASSOC) {
     foreach($line as $bs_trim) {
         //Trim whitespace from end of query
         $bs_trim = rtrim($bs_trim);
         echo "Clean!".$bs_trim;
     }
}
herriedm at yahoo dot com
26-Jun-2003 12:21
[Editor's Note: NULLs *are* distinguishable from 0.  See the manual section on comparison operators. ]

microsoft sql 2000 server
php running on windows 2000

Using the $row = mssql_fetch_array($result)

A null value will return 0, this is a problem if you are using 0 and some identifier for a value.  you should convert all nulls to something like -1 or "NO VALUE"
flatcable2000 at yahoo dot com
16-Jan-2003 01:22
Apparently php 4.0.6 does not support the second parameter. When used the error sounds
"Wrong parameter count for mssql_fetch_array"
shaver at qspeed dot com
11-Jan-2003 06:08
Note that the second parameter is the same as the msql version of this function (http://www.php.net/manual/en/function.msql-fetch-array.php) with the constants (MSQL_ASSOC, MSQL_NUM, and MSQL_BOTH) changing to MSSQL_.

Also note that if you don't specify this parameter you'll get both the numbers and column names, which can cause some frustration.
ask at manniac dot de
05-Jun-2002 09:13
It seems that this function creates arrays wich keys can not be longer than 30 chars. So when you're having a DB-field like "this_is_a_very_long_db_field_name"
its array-key is cut off after 30 chars and you will have to access the value with
rs["this_is_a_very_long_db_field_n"]