Lostnode Posted October 29, 2010 Share Posted October 29, 2010 Ok, so this seems kinda vague, cus normally thats what you do, however I am talking about a differnt kinds of output from a query... Here is what i am a talking about: My Query Code: $query = "SELECT * FROM settings WHERE storeid='store350' "; $which = $handle_db2; $result = mysql_query($query,$which); $num = mysql_num_rows ($result); $id = mysql_result($result,$i,"id"); $storeid = mysql_result($result,$i,"storeid"); $tinypass = mysql_result($result,$i,"tinypass"); $taxsetting = mysql_result($result,$i,"taxsetting"); $gst = mysql_result($result,$i,"gst"); $pst = mysql_result($result,$i,"pst"); $hst = mysql_result($result,$i,"hst"); $canpar = mysql_result($result,$i,"canpar"); $cp_fuelcharge = mysql_result($result,$i,"cp_fuelcharge"); $cp_markup = mysql_result($result,$i,"cp_markup"); $fedex = mysql_result($result,$i,"fedex"); $fe_fuelcharge = mysql_result($result,$i,"fe_fuelcharge"); $fe_markup = mysql_result($result,$i,"fe_markup"); $dhl = mysql_result($result,$i,"dhl"); $dh_fuelcharge = mysql_result($result,$i,"dh_fuelcharge"); $dh_markup = mysql_result($result,$i,"dh_markup"); $tnt = mysql_result($result,$i,"tnt"); $tn_fuelcharge = mysql_result($result,$i,"tn_fuelcharge"); $tn_markup = mysql_result($result,$i,"tn_markup"); As you can see, my variables are the same as my table column names... now if this "project" goes thorugh I could have hundreds of columns, I would hate to have to define each one making my file even larger. Is there a way to make it dynamically create the variables via an array or something Theoriticaly something like this: $query = "SELECT * FROM settings WHERE storeid='store350' "; $which = $handle_db2; $result = mysql_query($query,$which); $num = mysql_num_rows ($result); (While loop here) $Table_name_var = mysql_result($result,$i,"table_name"); (end while loop) Possible? Quote Link to comment https://forums.phpfreaks.com/topic/217244-is-there-a-way-to-make-rsults-from-sql-query-into-an-array/ Share on other sites More sharing options...
BlueSkyIS Posted October 29, 2010 Share Posted October 29, 2010 extract? <?php $query = "SELECT * FROM settings WHERE storeid='store350' "; $which = $handle_db2; $result = mysql_query($query,$which) or die(mysql_error()); $num = mysql_num_rows ($result); if ($num > 0) { $data = mysql_fetch_assoc($result); extract($data); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/217244-is-there-a-way-to-make-rsults-from-sql-query-into-an-array/#findComment-1128175 Share on other sites More sharing options...
Lostnode Posted October 29, 2010 Author Share Posted October 29, 2010 extract? <?php $query = "SELECT * FROM settings WHERE storeid='store350' "; $which = $handle_db2; $result = mysql_query($query,$which) or die(mysql_error()); $num = mysql_num_rows ($result); if ($num > 0) { $data = mysql_fetch_assoc($result); extract($data); } ?> Ok, would this make it so that I can use the column names as variables? for instance if I changed my code to the above, would the variable $dh_markup exist with the value of the dh_markup column from my db? Quote Link to comment https://forums.phpfreaks.com/topic/217244-is-there-a-way-to-make-rsults-from-sql-query-into-an-array/#findComment-1128188 Share on other sites More sharing options...
Lostnode Posted October 31, 2010 Author Share Posted October 31, 2010 Bump Any way to do this at all? Quote Link to comment https://forums.phpfreaks.com/topic/217244-is-there-a-way-to-make-rsults-from-sql-query-into-an-array/#findComment-1128590 Share on other sites More sharing options...
Pikachu2000 Posted October 31, 2010 Share Posted October 31, 2010 Did the code BlueSkyIS posted not work? Quote Link to comment https://forums.phpfreaks.com/topic/217244-is-there-a-way-to-make-rsults-from-sql-query-into-an-array/#findComment-1128676 Share on other sites More sharing options...
mannyee Posted October 31, 2010 Share Posted October 31, 2010 well theres another way too.... you can try this: foreach($array_results as $key => $val){ $$key = $val; } so your column names (the associative index) will be variables that hold the values Quote Link to comment https://forums.phpfreaks.com/topic/217244-is-there-a-way-to-make-rsults-from-sql-query-into-an-array/#findComment-1128677 Share on other sites More sharing options...
jcbones Posted October 31, 2010 Share Posted October 31, 2010 well theres another way too.... you can try this: foreach($array_results as $key => $val){ $$key = $val; } so your column names (the associative index) will be variables that hold the values That is what extract() does. Quote Link to comment https://forums.phpfreaks.com/topic/217244-is-there-a-way-to-make-rsults-from-sql-query-into-an-array/#findComment-1128679 Share on other sites More sharing options...
mannyee Posted October 31, 2010 Share Posted October 31, 2010 That is what extract() does. well indeed... .but theres some advantage of using loop over extract().... you might already know it but anyways heres a link i found....: http://davidwalsh.name/convert-key-value-arrays-standard-variables-php besides lostnode wanted to know the other way too....i guess..!! Quote Link to comment https://forums.phpfreaks.com/topic/217244-is-there-a-way-to-make-rsults-from-sql-query-into-an-array/#findComment-1128683 Share on other sites More sharing options...
Lostnode Posted November 1, 2010 Author Share Posted November 1, 2010 Thank you for the responses, I hadn't tested it out but I saw that somewhere in my code was a snippet that does that extract and it seems to work fine. I will test it out when I have a chance. As I have seen it work in another file, I am marking this solved. Quote Link to comment https://forums.phpfreaks.com/topic/217244-is-there-a-way-to-make-rsults-from-sql-query-into-an-array/#findComment-1128841 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.