Jump to content


Photo

multidim array query function


  • Please log in to reply
No replies to this topic

#1 Mr_Pancakes

Mr_Pancakes
  • Members
  • PipPip
  • Member
  • 17 posts
  • LocationSouthwest USA

Posted 15 September 2006 - 03:12 PM

i've written a function to help me store the results of a sql query into a multidimensional array. the function seems to be working ok, but i was wondering if there is a better way to do this from an OBDC referenced database? did i miss anything?
function execute_sql($cmd){

	$connection = db_connect();
	$rs = odbc_exec($connection,$cmd);
	if (!$rs){ exit("Error in SQL"); }
		
	if (odbc_fetch_row($rs)){
		$rows = odbc_num_rows($rs);

		if ($rows <= 1){
			$answer[] = odbc_fetch_array($rs, 1); }
		else {
			for ($k = 0; $k <= $rows; $k++){
				$answer[] = odbc_fetch_array($rs, $k); }
		}
	}
		
	odbc_close($connection);
	if (!empty($answer)) { return $answer; }
	else { return false; }
}

if we're querying for say: fname, lname; this returns a result in the form of:
print $answer[0]['fname'] . $answer[0]['lname']; // John Doe
print $answer[1]['fname'] . $answer[1]['lname']; // Jane Dough
print $answer[2]['fname'] . $answer[2]['lname']; // Mister Tea
// ...etc.

any ideas what im missing?
-steve




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users