Jump to content

storing database reults in an array problem


geroido

Recommended Posts

Hi

I'm looking for an idea of how to store  results from my  database in an array.  I'm having difficulty finding anything. Here's what bI need. Let's say I'm retrieving results from my database. I want to assign each of these results to an array. I've no problem with retrieving the records but need a hint on how to put these in the array. If I carry out a count of the records and then create an array with that count number, how do I place each record item in an array? Any ideas?

Link to comment
Share on other sites


$result = mysql_query("select * from `videos` where `type`='adult'");
$geroidosCollection = array();

while($video = mysql_fetch_assoc($result)) $geroidosCollection[] = $video;

 

Generally it is a bad idea to put large queries into arrays, especailly if you just want to then loop through that same array and do something.

Link to comment
Share on other sites

Hi tibberous

I'm looking for a way to do something and I'm trying to figure it out. Perhaps the array is not the best way. Can I explain the problem? I have a table in my database which looks like this

 

Ordernum  MenuItemID  ItemQuantity itemcost

621              70              3                5.50

621              69              1                14.99

621              53              2                12.99

777              13              4                10.99

777              25              3                11.50   

 

The Ordernum is important here. I want to scan through the table by Ordernum, multipy ItemQuantity by itemcost and them sum the total of the result. So the three results as above would be 16.50, 14.99 and 25.98. The sum of these would be 57.47.  For the second one(777), it would be 43.96 and 34.50. The sum is 78.46. I'm wondering how to do this with a query. I thought I would use an array to capture the distinct Ordernum(621 and 777) then go back through the table multiplying and summing for each unique Ordernum. Is this the best way?

Link to comment
Share on other sites

Hi Barand

Sorry for the delay in replying. Thanks for the info. I have a couple of questions. Will this sql statement multiply ItemQuantity by itemcost and add the results together? So in the example provided for Ordernum 621, I need the statement to multiply ItemQuantity * itemcost for all 3 records and add the 3 results together to give me a result for this order. It must do the same for Ordernum the two instances of 777 aswell and so on for any other orders. Can you help?

 

One last thing. I spent ages yesterday trying to find out how to capture the result of a 'sum' in a variable. E.g. you have in your example SUM(ItemQuantity * itemcost). What code do I then need to capture this result. I'm using php. Went through google for hours and no luck.

 

Thanks

Link to comment
Share on other sites

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.