note 60048 added to function.odbc-fetch-into

From: Date: Fri, 23 Dec 2005 22:46:42 +0000
Subject: note 60048 added to function.odbc-fetch-into
Groups: php.notes 
Request: Send a blank email to php-notes+get-100641@lists.php.net to get a copy of this message
Personally, I like the mssql_fetch_object function for returning rows from MSSQL, but sometimes need to use ODBC and I really miss this ability. The function below named odbc_fetch_object should return a similar result. The following is based on code I use but I haven't tested this example. <?php function run_odbc_query() { // Connect to the database $link = odbc_pconnect ("DSN", "login", "password", SQL_CUR_USE_ODBC); // If the $link isn't valid, quit if (!$link) { $dberror = "Couldn't connect to Database server!"; print $dberror; odbc_close($link); return; } // Define query $querystring = "select something from table"; // Run the query $result = odbc_exec($link, $querystring); // if there is a result, let's process it if(isset($result) && $result) { $objectresult=odbc_fetch_object($result); } $count = 0; while($count < count($objectresult)) { print $objectresult[$count]->something; $count++; } } function odbc_fetch_object($query) { // Get the field names and put them in an array for($i=1; $i <= odbc_num_fields($result); $i++) $varname[] = odbc_field_name($result, $i); // Loop to get the query result rows $count1 = 0; while (odbc_fetch_into($result, $result_array)) { $count2 = 0; while($count2 < count($result_array)) { // Assign the row values to the array variables $row_meta[$count1]->{$varname[$count2]} = $result_array[$count2]; $count2++; } $count1++; } return $row_meta; } ?> ---- Manual Page -- http://www.php.net/manual/en/function.odbc-fetch-into.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+60048 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+60048&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+60048&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+60048&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+60048&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+60048&report=yes&reason=non-english Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+60048&report=yes&reason=already+in+docs Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+60048&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+60048&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#100641) next »