note 27263 modified in function.odbc-fetch-row by danbrown

From: Date: Fri, 25 Jun 2010 15:09:43 +0000
Subject: note 27263 modified in function.odbc-fetch-row by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-170430@lists.php.net to get a copy of this message
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; } } ?> --was-- /* I found the combination of the following lines very useful, as the fieldnames will be simply available as variables by their names. $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. array obdc_fetch_assoc(resource result) */ 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; } } http://php.net/manual/en/function.odbc-fetch-row.php

« previous php.notes (#170430) next »