Jump to content

multidim array query function


Mr_Pancakes

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?
[code]
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; }
}
[/code]

if we're querying for say: fname, lname; this returns a result in the form of:
[code]
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.
[/code]

any ideas what im missing?
-steve
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.