ODBC 函数
在线手册:中文 英文
PHP手册

odbc_fetch_array

(PHP 4 >= 4.0.2, PHP 5)

odbc_fetch_arrayFetch a result row as an associative array

说明

array odbc_fetch_array ( resource $result [, int $rownumber ] )

Fetch an associative array from an ODBC query. See the changelog below for when this function is available.

参数

result

The result resource from odbc_exec().

rownumber

Optionally choose which row number to retrieve.

返回值

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

更新日志

版本 说明
4.3.3 This function exists when compiled with IBM DB2 or UnixODBC support.
4.3.2 This function exists when compiled for Windows.
4.0.2 This function exists when compiled with DBMaker support.

参见


ODBC 函数
在线手册:中文 英文
PHP手册
PHP手册 - N: Fetch a result row as an associative array

用户评论:

Vadim Istratov (10-Nov-2010 12:21)

Be careful while using this function with MSSQL for fetching results of SQL queries like following: "SELECT * FROM table". In this case array will NOT contain named indexes!

I.e.:
Table:
------------------------
| col1 | col2 | col3  |
------------------------
|   1   |   2   |   3   |
------------------------
|   4   |   5   |   6   |
------------------------

Code:
---------------------
<?php
$query
= "SELECT * FROM table";
$res = odbc_exec($query);

while(
$row = odbc_fetch_array($res) ) {
   
print_r($row);
}
?>
---------------------

Output will be:
---------------------
Array(
 [0] => 1,
 [1] => 2,
 [2] => 3
)
Array(
 [0] => 4,
 [1] => 5,
 [2] => 6
)
---------------------

No field names here!

armando dot ortiz at cppcorp dot com (21-Jan-2010 12:20)

In trying to execute queries and looping through the results like so...

<?php
while( odbc_fetch_row( $myResultID ) )
{
 
$myRow = odbc_fetch_array( $myResultID );
 
$thisResult[ $myRow[ "column" ] ] = $myRow;
}
?>

...I found that not only was my first row being skipped, but also every OTHER row was being skipped, thus a known result set containing 14 rows would dwindle down to 7.

My solution is as follows as I needed to return an array of rows:

<?php
while( $myRow = odbc_fetch_array( $myResultID ) )
{
 
$thisResult[ $myRow[ "column" ] ] = $myRow;
}
?>

James Auxier (27-Mar-2009 06:30)

Some interesting behavior was discovered while attempting simultaneous queries to a MS SQL server through the ODBC/freeTDS layer on SUSE on a persistent connection with ODBC cursors enabled. Stepping through the first resource with odbc_fetch_array() in a while loop works as expected, but calls to subsequently created resources within that loop were failing to return data where MySQL equivalents to the script were working just fine.

I found that adding a num_rows function call [custom function that simply steps through the result set... the misbehavior of odbc_num_rows() is another matter] immediately following each odbc_exec() then resetting the cursor seemed to cache the results for proper performance of the multiple active result sets.  It may be slightly slower than the MySQL equivalent, but at least it works.

michael dot buergi at zhdk dot ch (08-May-2008 10:30)

I use an odbc connection to a mssql2000 db. As long as I had set ansi_warnings on, I occasionnally got the last record twice when retrieving them with odbc_fetch_array or odbc_fetch_row and odbc_result.

bryant at zionprogramming dot com (20-Mar-2008 11:04)

I found the function submitted by jezndiatyahoodotcodotuk to be very helpful.  I'm using PHP 5.2.5 and this function isn't defined, so it may depend on the ODBC driver being used.

The only problem with the solution already posted is that the return values don't match the ones specified by the documentation.  I made the following modification so that the function will work the same whether it exists internally or not:

<?php
if (!function_exists('odbc_fetch_array')) {
    function
odbc_fetch_array($result, $rownumber=null) {
       
$array = array();
        if (!(
$cols = odbc_fetch_into($result, $result_array, $rownumber))) {
            return
false;
        }
        for (
$i = 1; $i <= $cols; $i++) {
           
$array[odbc_field_name($result, $i)] = $result_array[$i - 1];
        }
        return
$array;
    }
}

?>

luke at croslow dot com (23-Jul-2007 06:18)

On IBM DB2 for iSeries I have to specify a specific row number to retrieve. If I don't specify the row number, the behavior is erratic.

This seems to work:

<?php
for($i=1;$row=odbc_fetch_row($result,$i);$i++) {
 
//use $row
}
?>

The row count MUST start at 1 or else the behavior seems undefined; i.e., when I start from 0 some rows might be returned two or more times or not at all.

robincw at gnospammail dot com (09-Jan-2007 12:46)

I am using MS SQL Server 2005, and using odbc_connect with SQL_CUR_USE_ODBC.

I had a problem with odbc_fetch_array when getting results from a stored procedure that returns a text column. The misleading error I got was:

odbc_fetch_array() [function.odbc-fetch-array]: SQL error: [Microsoft][ODBC Cursor Library] Result set was not generated by a SELECT statement, SQL state SL004 in SQLGetData

Eventually I found that by converting the text column in my database to varchar(8000), it worked fine. Perhaps using CONVERT or CAST might have worked too. I also found that varchar(max) columns were scrambled.

tim at tmcode dot com (15-Sep-2005 04:07)

Depending on the odbc driver/operating system you are using,  odbc_fetch_array might not be defined. (ie if you used --with-custom-odbc you may be out of luck).   The following can be safely added to define the odbc_fetch_array function if doesn't exist already.  Unlike some of the other examples contributed here, this one will create the numeric keys and the text keys.   This function is definitely not very optimal because it has to check the number of fields and field names each time a row is pulled.  Often you will be looping through a result set and there is no need to pull this information each time.  It could probably be improved by caching the result of these functions but I'll leave that as an exercise for someone else... :)

if(!function_exists("odbc_fetch_array"))
{
   
    function odbc_fetch_array($res)
    {
        // make sure to pass back the false if we are out of rows
        if(!odbc_fetch_row($res)) return false;
       
        $row=array();

        // populate the row array
        $numfields=odbc_num_fields($res);
        for($i=1; $i<=$numfields; $i++)
        {
            //odbc starts its indice at 1 but since I am
            // trying to emulate the functionality of *_fetch_array
            // for other dbs (ie mysql)  I'm going to decrement my
            // my numeric indice by 1.  This might not be what
            // you are after in which case get rid of the -1 
            $row[odbc_field_name($res,$i)]=$row[$i-1]=odbc_result($res,$i);
           
        }
        return $row;
    }
   
   
}

javiles dot lazcano at gmail dot com (15-Sep-2005 03:24)

Hi. And using the same example to connect to a SYBASE ASE 12.0 Server with ODBC and Stored procedures, we have the following code:

<?PHP
function sybase_rs($SP){ 
  include(
"INCLUDE/sdd.odbc.conect.php");
 
$ARRAY    = array();
 
$RS        = odbc_exec($conexion,$SP);
 
$CONT        = 1;
  while (
odbc_fetch_row($RS))
         {
         for (
$y = 1; $y <= odbc_num_fields($RS); $y++)
              echo
odbc_result($RS,$y);
             
$CONT++;
            }
    if (
$CONT > 1)

}

$r = sybase_rs("{CALL dbo.sdd_determina_usuario ('". $_REQUEST['usuario']."','". $_REQUEST['clave']."')}");
echo
$r;         
?>

Remy (12-Apr-2005 01:35)

I use this function to get my recordset in an array, and it's very easy to use.

function dbquery($sql) {
  $arr    = array();
  $conn = odbc_connect('dsn','user','pass');
  $rs     = odbc_exec($conn,$sql);
  $x      = 1;
  while (odbc_fetch_row($rs)) {
    for ($y = 1; $y <= odbc_num_fields($rs); $y++)
      $arr[$x][$y] = odbc_result($rs,$y);
    $x++;
  }
  if ($x > 1)
    return $arr;
}

usage:

$arr=dbquery("SELECT * FROM tblTable");
echo $arr[1][1] //echo's first column of the first row

philip (22-Mar-2005 04:03)

This function requires one of the following to exist: Windows, DB2, or UNIXODBC.

miles at e-clipseconsulting dot com (15-Dec-2004 09:47)

In response to Sena's post, odbc_num_rows does not always return a row count.  When executing a query on MS Access it seems to return a row count of -1.

<?php

while(odbc_num_rows($myodbcexec)){
      
$myarray[] = odbc_fetch_array($myodbcexec);
}

?>

Just something to watch out for when attempting to execute this code.

Miles Phillips
e-Clipse Consulting

russ at russprince dot com (12-Nov-2004 02:57)

I really liked Ryan's example so I took it another step and added a recordset class to work with the connection class.  I made slight alterations to the original code as well.  Also note the recordset class takes advantage of php5's __get property function...

<%
class odbcRecordset {
   var $recordcount;
   var $currentrow;
   var $eof;

   var $recorddata;
   var $query;

   function odbcConnection(){
      $this->recordcount = 0;
      $this->recorddata = 0;
   }

   function SetData( $newdata, $num_records, $query ) {
      $this->recorddata = $newdata;
      $this->recordcount = $num_records;
      $this->query = $query;
      $this->currentrow = 0;
      $this->set_eof();
   }

   function set_eof() {
      $this->eof = $this->currentrow >= $this->recordcount;
   }

   function movenext()  { if ($this->currentrow < $this->recordcount) { $this->currentrow++; $this->set_eof(); } }
   function moveprev()  { if ($this->currentrow > 0)                  { $this->currentrow--; $this->set_eof(); } }
   function movefirst() { $this->currentrow = 0; set_eof();                                               }
   function movelast()  { $this->currentrow = $this->recordcount - 1;  set_eof();                         }

   function data($field_name) {
      if (isset($this->recorddata[$this->currentrow][$field_name])) {
         $thisVal = $this->recorddata[$this->currentrow][$field_name];
      } else if ($this->eof) {
         die("<B>Error!</B> eof of recordset was reached");
      } else {
         die("<B>Error!</B> Field <B>" . $field_name . "</B> was not found in the current recordset from query:<br><br>$this->query");
      }

      return $thisVal;
   }

   function __get($field_name) {
      return $this->data($field_name);
   }
}

class odbcConnection {
   var $user;  //Username for the database
   var $pass; //Password

   var $conn_handle; //Connection handle
   var $temp_fieldnames; //Tempory array used to store the fieldnames, makes parsing returned data easier.
  
   function odbcConnection(){
      $this->user = "";
      $this->pass = "";
   }
  
   function open($dsn,$user,$pass){
      $handle = @odbc_connect($dsn,$user,$pass,SQL_CUR_USE_ODBC) or
         die("<B>Error!</B> Couldn't Connect To Database. Error Code:  ".odbc_error());
      $this->conn_handle = $handle;
      return true;
   }
  
   function &execute($query){
      //Create a temp recordset
      $newRS = new odbcRecordset;
      $thisData = "";

      $res = @odbc_exec($this->conn_handle,$query) or
         die("<B>Error!</B> Couldn't Run Query:<br><br>" . $query . "<br><br>Error Code:  ".odbc_error());
      unset($this->temp_fieldnames);

      $i = 0;
      $j = 0;
      $num_rows = 0;

      // only populate select queries
      if (stripos($query, 'select ') !== false) {
         while(odbc_fetch_row($res)) {
            $num_rows++;
  
            //Build tempory
            for ($j = 1; $j <= odbc_num_fields($res); $j++) {
               $field_name = odbc_field_name($res, $j);
               $this->temp_fieldnames[$j] = $field_name;
               $ar[$field_name] = odbc_result($res, $field_name) . "";
            }
  
            $thisData[$i] = $ar;
            $i++;
         }
      }
     
      //populate the recordset and return it
      $newRS->SetData( $thisData, $num_rows, $query );
      return $newRS;
   }
}
%>

usage is pretty simple:

<%
  $con = new odbcConnection
  $con->open("dsn","user","pass")

  $sql = "select bar from foo";
  $rs = $con->execute($sql);

  if (!$rs->eof) {
    print $rs->data("bar");
      // or //
    print $rs->bar;
  }

  while (!$rs->eof) {
    // blah blah code
    $rs->movenext();
  }
%>

Works pretty well, but I haven't thoughly tested it yet.
Code can be dl'd here:

http://www.russprince.com/odbc_functions.zip

Cheers,
Russ

Ryan (rystar_x2000 at hotmail dot com) (10-Apr-2004 02:39)

Heya,

After much fiddling around with the odbc_num_rows() function, and trying to get odbc_fetch_array() to play nice with it, I decided to write this little function which will return a 2-dimensional array, in the format [rownumber][field].

The 'rownumber' array index is obviously the number of the row stored in the array.
The 'field' index is keyed to the field name on that row.

--Function--
<?

function fetch2DArray($res){   
    $i = 0;
    $j = 0;
    $toReturn = "";
   
    while(odbc_fetch_row($res))
    {
        for ($j = 1; $j <= odbc_num_fields($res); $j++)
        {       
             $field_name = odbc_field_name($res, $j);
             $ar[$field_name] = odbc_result($res, $field_name);
        }
       
        $toReturn[$i] = $ar;
        $i++;
    }
    return $toReturn;   
}
?>

I then took this one stage further.. and wrote a whole class to deal with the odbc connection... it's a bit slap-happy, but it does work well enough.

-- Class --
<?
class odbcConnection{
   var $user;  //Username for the database
   var $pass; //Password
   var $conn_handle; //Connection handle
   var $temp_fieldnames; //Tempory array used to store the fieldnames, makes parsing returned data easier.

   function odbcConnection(){
       $this->user = "";
       $this->pass = "";
   }
  
       function connectDatabase($dsn_link,$user,$pass){
        $handle = @odbc_connect($dsn_link,$user,$pass,SQL_CUR_USE_DRIVER) or die("<B>Error!</B> Couldn't Connect To Database. Error Code:  ".odbc_error());
        $this->conn_handle = $handle;
        return true;
    }
   
    function runStoredQuery($query, $returns_results){
   
    if($returns_results == false){
            return false;
        }
   
    $toReturn = "";
        $res = @odbc_exec($this->conn_handle, "exec ".$query."") or die("<B>Error!</B> Couldn't Run Stored Query. Error Code:  ".odbc_error());
        unset($this->temp_fieldnames);
            $i = 0;
            $j = 0;
           
            while(odbc_fetch_row($res))
            {
                  //Build tempory
                for ($j = 1; $j <= odbc_num_fields($res); $j++)
                   {       
                     $field_name = odbc_field_name($res, $j);
                     $this->temp_fieldnames[$j] = $field_name;
                      $this->temp_fieldnames[$j];
                     $ar[$field_name] = odbc_result($res, $field_name);
                   }
               
                $toReturn[$i] = $ar;
                $i++;
             }
   
     return $toReturn;
    }
   
    function runSQL($query,$returns_results){
    $toReturn = "";
   
        $res = @odbc_exec($this->conn_handle,$query) or die("<B>Error!</B> Couldn't Run Query. Error Code:  ".odbc_error());
           unset($this->temp_fieldnames);
        if($returns_results == false){
            return false;
        }
       
            $i = 0;
            $j = 0;
           
            while(odbc_fetch_row($res))
            {
                //Build tempory
                for ($j = 1; $j <= odbc_num_fields($res); $j++)
                   {       
                     $field_name = odbc_field_name($res, $j);
                     $this->temp_fieldnames[$j] = $field_name;
                     $ar[$field_name] = odbc_result($res, $field_name);
                   }
               
                $toReturn[$i] = $ar;
                $i++;
             }
           
     return $toReturn;
    }
   

}

//And an example of how to use the class:

include("dbClass.inc");  //Where dbClass.inc is the name of the file holding the class

//Declare a new instance of the class
$dbConnection = new odbcConnection;

$dsn = "GroupWork";   //Your System DSN name to point to your database
$dbConnection->connectDatabase($dsn,"","");  //No username and password - read only access

echo"<BR><HR><B>Testing SQL</b><BR><BR>";
$query_result = $dbConnection->runSQL("SELECT * FROM Event WHERE Type = 'Sport' ORDER BY EDate ASC",true);

    if(!$query_result)
    {
        //No Results - Your Error Code Here
    }else{
        //Get the results
        $key = $dbConnection->temp_fieldnames;
        $rows = count($query_result);
        $keys = count($key);
        $i = 0;
       
       
        while($i < $rows){
            $j = 1;
            echo "Echoing Row $i:<BR>";
           
                while($j < $keys - 1){
                   
                    //$query_result[row][field];
                    $result = $query_result[$i][$key[$j]];
                    $field = $key[$j];
                    echo("Field <b>'".$field."'</b> : ".$result." <BR>");
                   
                    $j++;
                }
            echo "<BR>----<BR><BR>";
            $i++;
        }
    }
?>

Hope this was of some help. If anyone has any improvments to the class, please drop them by me.

powerstat at web dot de (27-Mar-2004 12:48)

This function is badly implemented, because the fetch_array versions for all other databases are storing the values as associative AND numeric key.
This one only stores as associative key :(
Someone should really cleanup the complete database api design!

xhat at NOSPAM dot hotmail dot com (24-Mar-2004 06:46)

This is a quick and dirty way to accomplish the same thing:

<?
while(odbc_fetch_row($result)) {
$var1 = odbc_result($result, "NAMEOFFIELD1");
$var2 = odbc_result($result, "NAMEOFFIELD2");
..... //as many vars as you have fields with data to capture

$array_of_results[] = compact('var1', 'var2','var3', etc, etc)
}
?>

Just turn each returned row's data into variables then use the compact().  Turns each variable name into a key and the vars value into the array value.  Makes a wonderful 2d array that you can walk easily and still use key values to get at data.