©
Dokumen ini menggunakan Manual laman web PHP Cina Lepaskan
(PHP 4 >= 4.0.2, PHP 5)
odbc_fetch_object — Fetch a result row as an object
$result
[, int $rownumber
] )Fetch an object from an ODBC query.
result
The result resource from odbc_exec() .
rownumber
Optionally choose which row number to retrieve.
Returns an object that corresponds to the fetched row, or FALSE
if there
are no more rows.
Note: This function exists when compiled with DBMaker, IBM DB2 or UnixODBC support.
[#1] philip [2005-03-21 20:04:54]
This function requires one of the following to exist: Windows, DB2, or UNIXODBC.
[#2] thorsten at rinne dot info [2003-10-15 02:26:25]
odbc_fetch_object() works nice with PHP 4.3.3 under W2K with IBM DB2 V.7.2 and V.8.1:
<?php
$conn = odbc_connect($db_name, $username, $password) or die(odbc_error_msg());
$sql = "SELECT * FROM TABLE";
$result = odbc_exec($conn, $sql);
while ($rows = odbc_fetch_object($result)) {
print $rows->COLUMNNAME;
}
odbc_close($conn);
?>
[#3] j dot a dot z at bluewin dot ch [2003-04-30 13:26:58]
hey "general at maccrafters dot com"
thank you very much for your code. it saved me time!
however i extended it a bit!
---------------------------------------------
function __odbc_fetch_object($res)
{
if( function_exists("odbc_fetch_object") )
return odbc_fetch_object($res);
$rs = array();
$rs_obj = false;
if( odbc_fetch_into($res, &$rs) )
{
foreach( $rs as $key=>$value )
{
$fkey = odbc_field_name($res, $key+1);
$rs_obj->$fkey = trim($value);
}
}
return $rs_obj;
}
---------------------------------------------
cheers, jaz
[#4] h4 at locked dot org [2003-03-11 06:28:02]
my 2 cents:
function data($res) {
$obj = new stdClass();
$data_array = array();
if (!odbc_fetch_into($res, $data_array)) {
return 0;
}
$num_fields = odbc_num_fields($res);
for ($i = 0;$i < $num_fields; $i++) {
$name = odbc_field_name($res, $i + 1);
if (!$name) {
return 0;
}
$obj->{$name} = $data_array[$i];
}
return $obj;
}
works fine for me (PHP 4.3.1)
[#5] charlesk at netgaintechnology dot com [2003-01-23 10:54:30]
I asked one of the developers to enable this function in the CVS. I tried it and it worked. I didnt do anything special. I was using a Microsoft Access ODBC driver that came with my Windows XP Pro Install.
I was using the Apache web server.
Charles
[#6] general at maccrafters dot com [2002-11-05 11:45:16]
Here's a bit of code I came up with tha behaves just like mysql_fetch_object()
function odbc_fetch_object($result)
{
$rs=array();
if(odbc_fetch_into($result,&$rs))
{
foreach($rs as $key=>$value)
{
$fkey=strtoupper(odbc_field_name($result,$key+1));
$rs_obj->$fkey = trim($value);
}
}
return($rs_obj);
}
Special thanks to previous posters for giving me a starting point for this code.
[#7] kynaston at yahoo dot com [2002-09-05 05:55:25]
If you're using Masoud's code in PHP4.2+, change the fifth line to:
odbc_fetch_into($result,&$rs);
(the order of arguments have changed)
[#8] Marcus dot Karlsson at usa dot net [2002-04-24 10:57:20]
It' possible to get both odbc_fetch_object() and odbc_fetch_array() to work just by removing #ifdef HAVE_DBMAKER/#endif in php_odbc.h line 216 (219) and the same in php_odbc.c line 87 (90) and 1229 (1380).
I've done this sucessfully in the PHP 4.2.0 release using ODBC towards a MySQL database.
I really can't understand why the #ifdef is there from the beginning, but they do have their reasons.
These were the files i "patched"
[#9] masuod_a at hotmail dot com [2002-02-22 12:29:27]
This function not availible in PHP 4.1.1 , you can try this :
if (function_exists(odbc_fetch_object))
return;
function odbc_fetch_object($result, $rownumber=1) {
$rs=array();
odbc_fetch_into($result, $rownumber,$rs);
foreach ($rs as $key => $value) {
$fkey=strtolower(odbc_field_name($result, $key+1));
$rs_obj->$fkey = $value;
}
return $rs_obj;
}
if you wanna use this function in a loop you must set rownumber parameter
you can't use this function like :
while ($myobj=odbc_fetch_object($res)) {
....
}