Jump to content


Photo

Effeciently Pushing SQL Results into an array


  • Please log in to reply
6 replies to this topic

#1 XenoPhage

XenoPhage
  • Members
  • PipPipPip
  • Advanced Member
  • 99 posts

Posted 28 February 2006 - 08:39 PM

Greetings,

I'm trying to determine if there's a better way to return results and place them into an array. Essentially, I'm just trying to write less code, and I'm curious if there's a better way to do this. An example will probably help. Today, I do it like this :

$query = 'SELECT id, desc FROM table';
$result = mysql_query($query) or die ( 'Error: Query: ' . mysql_error() );

// Place the results into an array
$id = Array();
$desc = Array();
while ($line = mysql_fetch_array($result)) {
   array_push($id, $line[0]);
   array_push($desc, $line[1]);
}

// Free the result
mysql_free_result($result);

// Assign the array to the smarty template
$smarty->assign('id', $id);
$smarty->assign('desc', $desc);

The purpose here is to assign this to a smarty template where the following would be used :

<select name='select_box'>
   {html_options values=$id output=$desc}
</select>

Any tips on reducing my line count on this?

Thanks!
--
[a href=\"http://blog.godshell.com\" target=\"_blank\"]XenoPhage[/a]
[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]Something mysterious is formed, born in the silent void. Waiting alone and unmoving, it is at once still and yet in constant motion. It is the source of all programs. I do not know its name, so I will call it the Tao of Programming.[/quote]

#2 hitman6003

hitman6003
  • Members
  • PipPipPip
  • Advanced Member
  • 1,807 posts

Posted 28 February 2006 - 09:12 PM

Well, your query is probably going to fail, desc is a reserved word in SQL.

Other than that, I don't think that you can really reduce that any more than it already is.

Not sure what your hoping for.

#3 XenoPhage

XenoPhage
  • Members
  • PipPipPip
  • Advanced Member
  • 99 posts

Posted 28 February 2006 - 09:24 PM

[!--quoteo(post=350367:date=Feb 28 2006, 04:12 PM:name=hitman6003)--][div class=\'quotetop\']QUOTE(hitman6003 @ Feb 28 2006, 04:12 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Well, your query is probably going to fail, desc is a reserved word in SQL.

Other than that, I don't think that you can really reduce that any more than it already is.

Not sure what your hoping for.
[/quote]

Well, the query was bogus, so that's not an issue. :)

I was really just wondering if this was the best way to do this or not.. Seems like a lot of code for a simple function.. But then, I guess a larger, more complex program is nothing more than a lot of these simple functions strung together.. :)
--
[a href=\"http://blog.godshell.com\" target=\"_blank\"]XenoPhage[/a]
[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]Something mysterious is formed, born in the silent void. Waiting alone and unmoving, it is at once still and yet in constant motion. It is the source of all programs. I do not know its name, so I will call it the Tao of Programming.[/quote]

#4 wickning1

wickning1
  • Members
  • PipPipPip
  • Advanced Member
  • 405 posts

Posted 28 February 2006 - 10:58 PM

No, you're doing it correctly. Of course, if you were using my framework the whole thing would go like this:
$data = $db->getallcolumns('SELECT id, desc FROM table');

// Assign the array to the smarty template
$smarty->assign('id', $data['id']);
$smarty->assign('desc', $data['desc']);

But you'd have to have my framework now wouldn't you? :o

I'll share if you PM me (Disclaimer: PHP5 only, but mysql or mysqli both supported).

#5 XenoPhage

XenoPhage
  • Members
  • PipPipPip
  • Advanced Member
  • 99 posts

Posted 01 March 2006 - 02:40 AM

Hrm.. framework.. That would be the new buzzword for an old-school API.. :) I'd be interested in checking it out, but I'm currently using PHP 4...

This new framework leverages the new OO capabilities in PHP 5?
--
[a href=\"http://blog.godshell.com\" target=\"_blank\"]XenoPhage[/a]
[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]Something mysterious is formed, born in the silent void. Waiting alone and unmoving, it is at once still and yet in constant motion. It is the source of all programs. I do not know its name, so I will call it the Tao of Programming.[/quote]

#6 wickning1

wickning1
  • Members
  • PipPipPip
  • Advanced Member
  • 405 posts

Posted 01 March 2006 - 04:51 AM

Well in my vocabulary the API is just the public methods, while the framework is the whole thing.

But yeah, it's 5-only because object orientation changed so much. I've thought about porting a version that will run on 4, but I haven't much looked into it yet. I didn't write a lot of PHP4, I didn't care for the language until 5 went stable.

I put some documentation online the other day, take a look if you like. [a href=\"http://mythgaming.net/php/docs/database/db.html\" target=\"_blank\"]http://mythgaming.net/php/docs/database/db.html[/a]

#7 XenoPhage

XenoPhage
  • Members
  • PipPipPip
  • Advanced Member
  • 99 posts

Posted 01 March 2006 - 01:24 PM

[!--quoteo(post=350509:date=Feb 28 2006, 11:51 PM:name=wickning1)--][div class=\'quotetop\']QUOTE(wickning1 @ Feb 28 2006, 11:51 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Well in my vocabulary the API is just the public methods, while the framework is the whole thing.
[/quote]

Ok, semantics.. :) Either way, it seems to be just a new buzzword for an old concept.. Kinda like AJAX is the new buzzword for javascript activates DHTML.. :)

[!--quoteo(post=350509:date=Feb 28 2006, 11:51 PM:name=wickning1)--][div class=\'quotetop\']QUOTE(wickning1 @ Feb 28 2006, 11:51 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
But yeah, it's 5-only because object orientation changed so much. I've thought about porting a version that will run on 4, but I haven't much looked into it yet. I didn't write a lot of PHP4, I didn't care for the language until 5 went stable.
[/quote]

I stuck with 4 because it seems to have a much larger install base. It's a little easier on me since I have to deal with servers that have 4 installed already and 5 probably isn't forthcoming for quite a while...

[!--quoteo(post=350509:date=Feb 28 2006, 11:51 PM:name=wickning1)--][div class=\'quotetop\']QUOTE(wickning1 @ Feb 28 2006, 11:51 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
I put some documentation online the other day, take a look if you like. [a href=\"http://mythgaming.net/php/docs/database/db.html\" target=\"_blank\"]http://mythgaming.net/php/docs/database/db.html[/a]
[/quote]

Excellent, thanks. This helps out a bit...
--
[a href=\"http://blog.godshell.com\" target=\"_blank\"]XenoPhage[/a]
[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]Something mysterious is formed, born in the silent void. Waiting alone and unmoving, it is at once still and yet in constant motion. It is the source of all programs. I do not know its name, so I will call it the Tao of Programming.[/quote]




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users