csdn产品小助手 2015-06-17 14:32 采纳率: 0%
浏览 522

Ajax从MySQL提取数据

I'm currently trying to set up an easy temperature monitoring system for our lab. It's controlled through a web interface which plots the temperature vs. time data in a Dygraph. The temperature data is written into a mySQL DB. I'm trying to add another input field which can be used to select a certain time range to plot. I'm currently using a jQuery datebox to select the time window which seems to work fine. I'm sending an AJAX request to a PHP script which queries the DB. Instead of showing me the right data points, the PHP script just outputs an internal server error (500). The script works fine when the $_POST section of the PHP and the data part of the jQuery script is deleted.

My most recent JS code is:

$("#UpdateGraph").on("click", function(){
    var startDate = $("#datestart").val();
    var endDate = $("#datestop").val();
    $.ajax({
    url: "updateGraph.php",
    data: {start : startDate,
           stop : endDate},
    datatype: "json",
    type: "POST",
    success: function(data) { DyGraph(data); }
    });
});

The jQuery code is housed in a $(document).ready() environment. The corresponding PHP script looks like this:

  <?php
 $conn = new PDO("mysql:host=localhost;dbname=temperature", "USERNAME", "PASSWORD");
 $results = array();

 $startDate = $_POST['start'];
 $stopDate = $_POST['stop']: 

 $query = "SELECT atime,temperature FROM temp WHERE atime BETWEEN '"  . $startDate . "' AND '"  . $stopDate . "';" ;
 $stmt = $conn->prepare($query);
 $stmt->execute();
 while($result = $stmt->fetch(PDO::FETCH_ASSOC)){
  $results[] = $result['atime'].",".$result['temperature'];
 }
 print_r(json_encode(implode("
+",$results)));
?>

Any advice what I'm doing wrong? Thanks a lot!

  • 写回答

1条回答 默认 最新

  • weixin_33701251 2015-06-19 07:23
    关注

    I guess, the source of your error is a syntax error in line $stopDate = $_POST['stop']: There is a colon at the end of the line. You should change it to semicolon.

    Syntax errors in php scripts at the server will show error 500 in http clients. I suggest you to use a linter to avoid this kind of errors. All the popular editors have plugins for this.

    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!