0
     id      timing     temp    temp1   temp2   temp3
   5260     1446746934  -76     -15      4       25
    5259    1446746332  -75     -14      5      25
    5258    1446745731  -77     -15      6      25

I have database like this. And I wrote the code below to get the row value out from my database. I would like to get the data from the previous points. Right now, I only get the latest point -76 from temp column. How to get the data from previous points like -75 or -77 at one column ??

  $qryd = "SELECT id, timing, temp,temp1,temp2,temp3 FROM tempArray WHERE timing>='$dlimit' ORDER BY id ASC LIMIT 1008;

    $i=0;
    $r = mysql_query($qryd);
    $count = mysql_num_rows($r);

    while($row=mysql_fetch_array($r, MYSQL_ASSOC)) {
      $tid=$row['id'];
     $dt = $row['timing'];
      $te = $row['temp'];

      $return="[$dt, $te],";
      echo $return;

      $i++;

    }
     echo $te;
//the result is -76
  • 5
    You fetch all the data but you echo only the last because echo $te; is outside the loop. – Kostas Mitsarakis Nov 05 '15 at 18:35
  • 3
    If you can, you should [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really not hard](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Nov 05 '15 at 18:44
  • 2
    do read those links Jay just mentioned. He has really good info on his website too, not to mention cool fonts :) – Drew Nov 05 '15 at 18:46
  • @KostasMitsarakis , how to get only -75 in my case ? – user2573051 Nov 05 '15 at 19:08
  • You have to add a WHERE clause to your query. – Kostas Mitsarakis Nov 05 '15 at 19:20
  • @KostasMitsarakis Sorry for the misleading question. my database is updated constantly. and I would like to always get the second point or third point value from the database. – user2573051 Nov 05 '15 at 19:26
  • I post posted an answer. Check it and inform me if this is what you need. – Kostas Mitsarakis Nov 05 '15 at 19:33
  • @Drew yes I just clicked the link, they are nice font's arn't they. Smooth to read like yoghurt for my eyes :-) – Martin Nov 05 '15 at 19:39
  • 1
    @Martin is was so smooth that I wish much was written with them :) – Drew Nov 05 '15 at 19:41
  • 1
    @Drew start a petition ;-) – Martin Nov 05 '15 at 19:49

2 Answers2

0

Try this:

$qryd = "SELECT id, timing, temp,temp1,temp2,temp3 FROM tempArray";

$i=0;
$r = mysql_query($qryd);
$count = mysql_num_rows($r);

while($row=mysql_fetch_array($r, MYSQL_ASSOC)) {
  $tid=$row['id'];
 $dt = $row['timing'];
  $te = $row['temp'];

  $return="[$dt, $te],";
  echo $return;

  $i++;
  echo $te;
}

The "echo $te" was outside the loop, so the result was complete but your code prints only the last record returned by the query execution.

Norman
  • 435
  • 6
  • 10
  • It display all the data (I have over 1000 data points). But how to get only -75 for this case. – user2573051 Nov 05 '15 at 19:00
  • Ok i have understand the example, but i do not understand if you want to display a specific record or something else like the last n-recrods or something like this. – Norman Nov 05 '15 at 19:13
  • Yes, I only want to display a specific record. – user2573051 Nov 05 '15 at 19:16
  • So wich condition you want to use to discriminate this record? For example " if ($te == -75) return $te " – Norman Nov 05 '15 at 19:46
  • I have thousands of data point. and each point is different value. I cannot make condition for that. every time the database updated, I want to have second value point. – user2573051 Nov 05 '15 at 22:39
0

The best way I can do is to do more code on my database site to grab data one by one. I know it is not the right way to do it. But at least it is the best I can do to my program.