Jump to content


Photo

MYSQL and PHP array


  • Please log in to reply
3 replies to this topic

#1 analyst

analyst
  • New Members
  • Pip
  • Newbie
  • 1 posts

Posted 19 February 2006 - 11:51 AM

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.

#2 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 19 February 2006 - 08:48 PM

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.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#3 wickning1

wickning1
  • Members
  • PipPipPip
  • Advanced Member
  • 405 posts

Posted 20 February 2006 - 07:17 PM

<?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);
?>


#4 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 20 February 2006 - 08:29 PM

That's what I'm talking about! BTW, I was under the impression that you needed to declare the arrays first -- but apparently not.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users