Jump to content

Archived

This topic is now archived and is closed to further replies.

analyst

MYSQL and PHP array

Recommended Posts

Hello to everybody, I'm new.
I've this problem.
I Have a MySQL DB with a table with financial data.
The table has five columns: DATE, OPEN, HIGH, LOW, CLOSE and thousand of rows.
To plot the data on a chart I need to get only last 50 rows of the table and put each column data in a single array.
Could you help me with a PHP code ?
Thanks in advance
A.

Share this post


Link to post
Share on other sites
Well, you can use a "LIMIT 50" clause to get only 50 rows, but you'll need to have an ORDER BY clause, probably by date descending, to get the most "recent" 50. As far as the array goes, simply create 5 empty arrays, and push onto each one as you iterate through the result set.

Share this post


Link to post
Share on other sites
[code]<?php
// open a mysql database connection
$result = mysql_query("SELECT * FROM myTable ORDER BY DATE DESC LIMIT 50", $dblink);
while ($row = mysql_fetch_array($result)) {
   $date[] = $row['date'];
   $open[] = $row['open'];
   $high[] = $row['high'];
   $low[] = $row['low'];
   $close[] = $row['close'];
}
mysql_free_result($result);
?>[/code]

Share this post


Link to post
Share on other sites
That's what I'm talking about! BTW, I was under the impression that you needed to declare the arrays first -- but apparently not.

Share this post


Link to post
Share on other sites

×

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.