Jump to content

Recommended Posts

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?

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("...");
}

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.

 

 

 

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

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.

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.