XenoPhage Posted February 28, 2006 Share Posted February 28, 2006 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 :[code]$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 resultmysql_free_result($result);// Assign the array to the smarty template$smarty->assign('id', $id);$smarty->assign('desc', $desc);[/code]The purpose here is to assign this to a smarty template where the following would be used :[code]<select name='select_box'> {html_options values=$id output=$desc}</select>[/code]Any tips on reducing my line count on this?Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/3774-effeciently-pushing-sql-results-into-an-array/ Share on other sites More sharing options...
hitman6003 Posted February 28, 2006 Share Posted February 28, 2006 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 Link to comment https://forums.phpfreaks.com/topic/3774-effeciently-pushing-sql-results-into-an-array/#findComment-13111 Share on other sites More sharing options...
XenoPhage Posted February 28, 2006 Author Share Posted February 28, 2006 [!--quoteo(post=350367:date=Feb 28 2006, 04:12 PM:name=hitman6003)--][div class=\'quotetop\']QUOTE(hitman6003 @ Feb 28 2006, 04:12 PM) [snapback]350367[/snapback][/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.. :) Quote Link to comment https://forums.phpfreaks.com/topic/3774-effeciently-pushing-sql-results-into-an-array/#findComment-13116 Share on other sites More sharing options...
wickning1 Posted February 28, 2006 Share Posted February 28, 2006 No, you're doing it correctly. Of course, if you were using my framework the whole thing would go like this:[code]$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']);[/code]But you'd have to have my framework now wouldn't you? :oI'll share if you PM me (Disclaimer: PHP5 only, but mysql or mysqli both supported). Quote Link to comment https://forums.phpfreaks.com/topic/3774-effeciently-pushing-sql-results-into-an-array/#findComment-13146 Share on other sites More sharing options...
XenoPhage Posted March 1, 2006 Author Share Posted March 1, 2006 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? Quote Link to comment https://forums.phpfreaks.com/topic/3774-effeciently-pushing-sql-results-into-an-array/#findComment-13190 Share on other sites More sharing options...
wickning1 Posted March 1, 2006 Share Posted March 1, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/3774-effeciently-pushing-sql-results-into-an-array/#findComment-13212 Share on other sites More sharing options...
XenoPhage Posted March 1, 2006 Author Share Posted March 1, 2006 [!--quoteo(post=350509:date=Feb 28 2006, 11:51 PM:name=wickning1)--][div class=\'quotetop\']QUOTE(wickning1 @ Feb 28 2006, 11:51 PM) [snapback]350509[/snapback][/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) [snapback]350509[/snapback][/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) [snapback]350509[/snapback][/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... Quote Link to comment https://forums.phpfreaks.com/topic/3774-effeciently-pushing-sql-results-into-an-array/#findComment-13270 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.