令人不安的PHP代码获得多重价值

时间:2013-11-25 18:41:43

标签: php mysql

我的代码应该从mysql数据库获取值,我只能得到一个值,但我有while循环所以它可以获取值和输出数据用逗号分隔。但我只得到一个值,不能得到多个值。结果应该是这样的,//结束主PHP块。数据如下所示:[[123456789,20.9],[1234654321,22.1]]这里是代码:

<?php
// connect to MySQL
mysql_connect('localhost','','') or die("Can't connect that way!");
@mysql_select_db('temperature1') or die("Unable to select a database called 'temperature'");
if(ISSET($_GET['t']) && (is_numeric($_GET['t']))  ){
  // message from the Arduino
  $temp = $_GET['t'];

  $qry = "INSERT INTO tempArray(timing,temp) VALUES(".time().",'$temp')";
  echo $qry;
  mysql_query($qry);
  mysql_close();
  exit('200');
}
// no temp reading has been passed, lets show the chart.

$daysec = 60*60*24; //86,400
$daynow = time();
if(!$_GET['d'] || !is_numeric($_GET['d'])){
  $dayoffset = 1;
} else {
  $dayoffset = $_GET['d'];
}
$dlimit = $daynow-($daysec*$dayoffset);

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

// 1008 is a weeks worth of data,
// assuming 10 min intervals
$r = mysql_query($qryd);

$count = mysql_num_rows($r);

$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']+36000)*1000;
  $te = $row['temp'];

  if($te>$maxtemp) $maxtemp=$te; // so the graph doesnt run along the top
  if($te<$mintemp) $mintemp=$te; // or bottom of the axis
  $return="[$dt, $te]";
  echo $return; //here I get all values [1385435831000, 21][1385435862000, 23][1385435892000, 22][1385435923000, 25][1385435923000, 22]
  $i++;
  if($i<$count) $return= ","; echo $return; // if there is more data, add a ','    however, it return me ,,,[1385435923000, 22] 
  $latest = "$dt|$te"; // this will get filled up with each one
}
mysql_close();
// convert $latest to actual date - easier to do it here than in javascript (which I loathe)
$latest = explode('|',$latest);
$latest[0] = date('g:ia, j.m.Y',(($latest[0])/1000)-36000);


?> 

1 个答案:

答案 0 :(得分:0)

你只是将$ return分配给你的while循环抓取的最后一行的值而不是连接它。试试这个

$return = "[";
while($row=mysql_fetch_array($r, MYSQL_ASSOC)) {
  $tid=$row['id'];
  $dt = ($row['timing']+36000)*1000;
  $te = $row['temp'];

 if($te>$maxtemp) $maxtemp=$te; // so the graph doesnt run along the top
 if($te<$mintemp) $mintemp=$te; // or bottom of the axis
 $return.="[$dt, $te]";
 $i++;
 if($i<$count) $return .= ", ";// if there is more data, add a ','
 $latest = "$dt|$te"; // this will get filled up with each one
}
$return .= "]";