doggee7 Posted November 4, 2008 Share Posted November 4, 2008 Hello everyone, im new to the board and about 3 weeks new to php. I'm trying to create a script that gets distinct production names from a database, and then creates queries based on those production names, and inserts the query results into an array within a for loop. Heres what i have now: $prods = mysql_query("SELECT DISTINCT 'production' FROM 'logs' WHERE 'drv_id' = '".$drv_name); for($p = 0; $prod2 = mysql_fetch_assoc($prods); $p++) { $prod_arr[$p] = $prods2['production']; $prod_result[$p] = mysql_query("SELECT * FROM 'logs' WHERE 'production' = '".$prod_arr[$p]); } I'm trying to do this, so that i can reflect the results in another loop i have further on down the script, that would utilize the $prod_result[$p] contents to popullate fields in a table. Please let me know if there is a way to store multiple mysql_query results into a single array (maybe i need to be using nested arrays somehow? Quote Link to comment https://forums.phpfreaks.com/topic/131288-storing-variable-query-results-into-an-array/ Share on other sites More sharing options...
JasonLewis Posted November 4, 2008 Share Posted November 4, 2008 You seem to be going about it incorrectly. You should use a while loop, that loops through all the results from the initial query. Don't worry about a counter as well. $prod_arr = array(); $prod_result = array(); while($prod2 = mysql_fetch_assoc($prods)){ $prod_arr[] = $prod2['production']; $prod_result[] = mysql_query("..."); } Quote Link to comment https://forums.phpfreaks.com/topic/131288-storing-variable-query-results-into-an-array/#findComment-681741 Share on other sites More sharing options...
laffin Posted November 4, 2008 Share Posted November 4, 2008 code may be different, but that for loop will work its not the php code that is the problem, but the queries themselves. I think u should visualize what data and how ya want the data either single or multiple array. a layout shud be able to descibe a lot better than wut that code did. Quote Link to comment https://forums.phpfreaks.com/topic/131288-storing-variable-query-results-into-an-array/#findComment-681749 Share on other sites More sharing options...
Adam Posted November 4, 2008 Share Posted November 4, 2008 By the way: $prod_result[] = mysql_query(".."); ...Won't be saving the database record as an array - like you may expect - but a bool (true/false) return from the mysql_query... you'd need to use mysql_fetch_assoc() again to return the actual row values.. Adam Quote Link to comment https://forums.phpfreaks.com/topic/131288-storing-variable-query-results-into-an-array/#findComment-681914 Share on other sites More sharing options...
JasonLewis Posted November 4, 2008 Share Posted November 4, 2008 mysql_query() in this instance would return a MySQL Resource, or false if the query failed. If you wanted to build your queries first, then loop through them and perform mysql_fetch_assoc() on the result then it should technically work. Although I've never tried it. Quote Link to comment https://forums.phpfreaks.com/topic/131288-storing-variable-query-results-into-an-array/#findComment-681916 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.