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

odbc_fetch_row

(PHP 4, PHP 5)

odbc_fetch_rowFetch a row

说明

bool odbc_fetch_row ( resource $result_id [, int $row_number ] )

Fetches a row of the data that was returned by odbc_do() or odbc_exec(). After odbc_fetch_row() is called, the fields of that row can be accessed with odbc_result().

参数

result_id

The result identifier.

row_number

If row_number is not specified, odbc_fetch_row() will try to fetch the next row in the result set. Calls to odbc_fetch_row() with and without row_number can be mixed.

To step through the result more than once, you can call odbc_fetch_row() with row_number 1, and then continue doing odbc_fetch_row() without row_number to review the result. If a driver doesn't support fetching rows by number, the row_number parameter is ignored.

返回值

Returns TRUE if there was a row, FALSE otherwise.


ODBC 函数
在线手册:中文 英文
PHP手册
PHP手册 - N: Fetch a row

用户评论:

furiousrcj (24-Jun-2010 04:02)

Here is code snippet to do dynamically build a table so you do not have to hardcode the fields that you wish to display. You could use the the odbc_result_all but this allows special handling of rows/columns. This is similar to some ASP code I use.

<?php
$Fields
= odbc_num_fields($cur);
print
"<table border='1' width='100%'><tr>";
// Build Column Headers
   
for ($i=1; $i <= $Fields; $i++){
   
printf("<th bgcolor='silver'>%s</th>", odbc_field_name( $cur,$i));
    }   
// Table Body
   
$Outer=0;
    while(
odbc_fetch_row( $cur )){
   
$Outer++;
    print
"<tr>";
    for(
$i=1; $i <= $Fields; $i++){
       
printf("<td>%s</td>", odbc_result( $cur, $i ));
        }
    print
"</tr>";
    }
print
"</table>";
print
"<b> Your request returned $Outer rows!</b>";
odbc_close( $cnx);
?>

GiladD (12-Jun-2008 04:12)

Here's my silly companion to odbc_fetch_row, odbc_fetch_column!

It takes a SQL table and the Column's name to look for, and returns an array with all the items in that column. It can be easily changes to take the column's number instead of name, but I prefer it this way.

<?php
function odbc_fetch_column($my_rs,$col_name) {
$tmpArray = array();
while (
odbc_fetch_row($my_rs)) {array_push($tmpArray,odbc_result($my_rs,$col_name));}
return
$tmpArray;
}
?>

ben at imperialsoftwaresystems dot com (14-Feb-2008 07:15)

I traced out a problem to odbc_fetch_row hanging while trying to retrieve the 'row after the last record'.  Instead of simply returning false it would cause a timeout.  This crept up after a site I maintain recently converted from MS Access DB to MS SQL Server DB.

To work around the issue I first do 'select count(*) from blah'.  I check that value as I loop through the records, and once I reach the number of records specified by my count(*) I stop my loop.

romanziak at yahoo dot ca (22-Mar-2006 01:50)

I see many here try to iterate to emulate tis missing functionality. I benchmarked the performance on my PentiumM 1.6G on Excel table with 1100 rows and 2 field records and got 82ms time to retrieve number of rows using iteration through all of them, but only 7ms to execute the SQL statement. In this case of thousadn(s) of rows, it is probably faster to have the database calculate rows.

Here is different algorithm for calculating the number of rows, which does not iterate through all of them, just uses interval splitting algorithm, and number of iterations is log2(n), where n is total number of rows in the result. The below algorithm in the case above (Excel table, 1100 rows 2 fields) took 10ms to calculate:

<?php
   
for($lo=0,$hi=1000000; $lo<$hi; )
    {
       
$num = (int)(($hi+$lo) / 2);
        if(
odbc_fetch_row($this->Result, $num))
           
$lo = $num + 1;
        else
           
$hi = $num - 1;
    }
?>

ricmailinglists at yahoo dot com dot br (09-Jan-2006 08:17)

To count all the row on a table

<?php

$connection
= odbc_connect("SolidBase", "user", "");

$query = "SELECT Codigo FROM Produto";

$queryexe = odbc_do($connection, $query);

$i = 0;

while(
odbc_fetch_row($queryexe)) $i++;

echo
$i;

?>

eolscr at gmail dot com (07-Apr-2005 04:05)

When I migrates from 4 to 5 took me a long day to find the solution.

The way to use it without problems

In php4:

<?php
while (odbc_fetch_row($stringsql)) {

// ...

}
?>

In php5:

<?php

odbc_fetch_row
($stringsql, 0);

while (
odbc_fetch_row($stringsql)) {

// ...

}

?>

Good luck

RRRRyan (24-Nov-2004 01:00)

It appears in PHP 5 that odbc_fetch_row($rs,0) no longer acts the same way. Now that I moved to PHP 5 moving to row 0 no longer resets the record set rather it fetches the first row. :-( This broke a lot of things for me. So watch out for a missing first row on record sets reset in this fashion.

alex at bartl dot net (29-Nov-2002 05:22)

I found the combination of the following lines very useful, as the
fieldnames will be simply available as variables by their names.

<?php
$query
="select fieldname from table";
// ...
$line=mysql_fetch_assoc($query_result);
extract($line);
echo
$fieldname;
?>

However, I was missing the function odbc_fetch_assoc() so I created it myself to use in in the same way like  mysql_fetch_assoc(). I am sure it could be coded in a better way, but at least it works. As a side effect $odbc_affected_rows should contain the amount of rows found, but have a look at odbc_num_rows() as well, as in my case it does not contain the expected information.

 
<?php array obdc_fetch_assoc(resource result); ?>


Code example:

<?php
function odbc_fetch_assoc($rs){
 if (
odbc_fetch_row($rs)){
 
$line=array("odbc_affected_rows"=>odbc_num_rows($rs));
  for(
$f=1;$f<=odbc_num_fields($rs);$f++){
  
$fn=odbc_field_name($rs,$f);
  
$fct=odbc_result($rs,$fn);
  
$newline=array($fn => $fct);
  
$line=array_merge($line,$newline);
  
//echo $f.": ".$fn."=".$fct."<br>";
 
}
  return
$line;
 }
 else{
  return
false;
 }
}
?>

scott at abcoa dot com (20-Sep-2002 08:42)

The odbc_fetch_row() function worked fine when I used PHP version 4.0.6 but when I upgraded PHP to 4.2.2, the odbc_fetch_row() doesn't work when there is 1 row. 

When I use the echo command to see the response to the odbc_fetch_row() before the while loop, it showed that it doesn't return True or anything when there is 1 row.

I made the workaround to the problem by including the "$bug_workaround" into the script, this is to be use as 'row number' as shown in the PHP manual at "http://www.php.net/manual/en/function.odbc-fetch-row.php".

I enclosed two clipping to point this out.  The 1st clipping is the one that don't work and the 2nd clipping showed the work-around to it.  In this script, you'll find the word, 'INQUIRIES', it is a table that contain number of companies and users.  What the script does is to display each user whenever the company is selected.  The cool thing about it is it won't display the data if there is no row for any user.

<?php
// --clip--
$cid = odbc_connect('blah blah blah');
$ask7 = "SELECT * FROM INQUIRIES WHERE USER_ID = '38SCK3'";
$R7 = odbc_exec($cid,$ask7);
$result = odbc_result($R7,1);

echo
"Result or No Result??? --> ".odbc_fetch_row($R7);

while (
odbc_fetch_row($R7))
{
 
odbc_fetch_into($R7,$inquiry,$inq_c);
 echo
$inquiry[0], $inquiry[1];
}
// --clip--

// --clip--
$cid = odbc_connect('blah blah blah');
$ask7 = "SELECT * FROM INQUIRIES WHERE USER_ID = '38SCK3'";
$R7 = odbc_exec($cid,$ask7);
$result = odbc_result($R7,1);

echo
"Result or No Result??? --> ".odbc_fetch_row($R7);

$bug_workaround=0;

while (
odbc_fetch_row($R7,++$bug_workaround))
{
 
odbc_fetch_into($R7,$inquiry,$inq_c);
 echo
$inquiry[0], $inquiry[1];
}
// --clip--
?>

I post this into bugs.php.net as well.  See bug #19528

david dot zepp at alltel dot com (01-Mar-2000 05:49)

Here is code snippet to do dynamically build a table so you do not have to hardcode the fields that you wish to display. You could use the the odbc_result_all but this allows special handling of rows/columns. This is similar to some ASP code I use.

<?php
$Fields
= odbc_num_fields($cur);
print
"<table border='1' width='100%'><tr>";
// Build Column Headers
   
for ($i=1; $i <= $Fields; $i++){
   
printf("<th bgcolor='silver'>%s</th>", odbc_field_name( $cur,$i));
    }   
// Table Body
   
$Outer=0;
    while(
odbc_fetch_row( $cur )){
   
$Outer++;
    print
"<tr>";
    for(
$i=1; $i <= $Fields; $i++){
       
printf("<td>%s</td>", odbc_result( $cur, $i ));
        }
    print
"</tr>";
    }
print
"</table>";
print
"<b> Your request returned $Outer rows!</b>";
odbc_close( $cnx);
?>