Jump to content


This topic is now archived and is closed to further replies.


multidim array query function

Recommended Posts

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); }

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?

Share this post

Link to post
Share on other sites


Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.