PHP Doku:: Liefert eine Datenzeile zurück - function.odbc-fetch-row.html

Verlauf / Chronik / History: (50) anzeigen

Sie sind hier:
Doku-StartseitePHP-HandbuchFunktionsreferenzDatenbankerweiterungenAbstraktionsebenenODBC (Unified)ODBC Funktionenodbc_fetch_row

Ein Service von Reinhard Neidl - Webprogrammierung.

ODBC Funktionen

<<odbc_fetch_object

odbc_field_len>>

odbc_fetch_row

(PHP 4, PHP 5)

odbc_fetch_rowLiefert eine Datenzeile zurück

Beschreibung

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

Wenn odbc_fetch_row() erfolgreich durchgeführt wurde (also eine oder mehrere Datenzeilen gefunden wurden), dann wird TRUE zurückgeliefert. Wenn keine (weitere) Zeile vorhanden ist, wird FALSE zurückgegeben.

odbc_fetch_row() stellt eine Zeile der von odbc_do()/odbc_exec() zurückgegeben Daten bereit. Nachdem odbc_fetch_row() aufgerufen wurde, können die Felder dieser Zeile mit odbc_result() genutzt werden.

Wenn row_number nicht definiert ist, versucht odbc_fetch_row() die nächste Zeile aus dem Abfrageergebnis result_id zu holen. odbc_fetch_row() kann abwechselnd ohne und mit dem Parameter row_number genutzt werden.

Um durch ein Abfrageergebnis mehrfach zu durchlaufen, kann man odbc_fetch_row() mit row_number gleich 1 aufrufen, um dann wie gewohnt mit odbc_fetch_row() ohne Zeilenparameter fortzufahren. Wenn der ODBC-Treiber die Herausgabe von bestimmten Zeilen nicht unterstützt, wird der Parameter ignoriert.


10 BenutzerBeiträge:
- Beiträge aktualisieren...
furiousrcj
24.06.2010 17: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.06.2008 17: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.02.2008 20: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.03.2006 14: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
9.01.2006 21: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
7.04.2005 17: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.11.2004 2: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.11.2002 18: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.09.2002 21: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
1.03.2000 18: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);
?>



PHP Powered Diese Seite bei php.net
The PHP manual text and comments are covered by the Creative Commons Attribution 3.0 License © the PHP Documentation Group - Impressum - mail("TO:Reinhard Neidl",...)