1

I am a very newbie on php and sql.

This is how the database look like.

I want the changes of Close for everyday.

 // Quantity of rows

 $sql = mysql_query("SELECT Date, Close FROM stock");

 $range = mysql_num_rows($sql);

echo $range . "<br>";

// Getting the Change

$result2 = mysql_fetch_array($sql);

for($i=1;$i<$range+1;$i++){

    $dayChange = $result2[$i]-$result2[$i-1];

    echo $dayChange . "<br>";

}

https://i.stack.imgur.com/KYMke.jpg

yunzen
  • 32,854
  • 11
  • 73
  • 106
Sean S
  • 11
  • 2
  • Count is better achieved by `SELECT COUNT(*) FROM stock`. And you should add LIMITS to your query of there ary many result rows and add a paging mechanism. – yunzen Dec 15 '13 at 13:58
  • @HerrSerker why is count better achieved by using count? Not arguing, just wondering why. – Ali Dec 15 '13 at 13:59
  • @AliTrixx If you have many result rows, it will slow down system, because all the rows must be send from MySQL server to the PHP process. – yunzen Dec 15 '13 at 14:01
  • I can't get the changes showing on the browser, I dont know whats going on – Sean S Dec 15 '13 at 14:05
  • 1
    [Please, don't use `mysql_*` functions in new code](http://stackoverflow.com/q/12859942). They are no longer maintained [and are officially deprecated](http://php.net/mysql_connect). Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://php.net/manual/en/mysqlinfo.api.choosing.php) will help you decide which. [Here is a good PDO tutorial](http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers). – vascowhite Dec 15 '13 at 14:05

1 Answers1

0

If there is no holiday then try this sql query:

SELECT id, sDate AS mDate, close, (
close - ( 
SELECT close
FROM stock
WHERE sDate = DATE_ADD( mDate, INTERVAL 1 
DAY ) )
) AS dayChange
Nouphal.M
  • 6,304
  • 1
  • 17
  • 28
  • There are trading days only. No Saturday and Sunday, No public holidays. Anymore help!? Thanks – Sean S Dec 16 '13 at 17:21