【问题标题】:Json string value in MySQLMySQL中的Json字符串值
【发布时间】:2015-12-07 10:59:38
【问题描述】:

我有一个 Arduino 可以创建一个 json 字符串:

{"sensorId":"[id sensore]","value":"[valore_letto_dal_sensore]"}

使用这个 Arduino 代码:

    client.println("POST *file PHP  * HTTP/1.1");
    client.println("Host: *URL web site*");
    client.println("Accept: */" "*");
    client.println("User-Agent: Arduino/1.0.0");
    client.println("Content-Type: application/json");
    client.print("Content-Length: ");
    client.println(post.length());
    client.println("Connection: close");
    client.println();
    client.print(post);

    post = "";

因此,使用 POST 发送到文件 .PHP 传感器名称和值。 你能帮我创建 db 和 PHP 文件吗?

我找到了一个使用 goole api http://blog.mikinacucchi.it/2013/08/rilevazione-temperatura-arduino-php-json-googlechart/ 创建图表的网站

【问题讨论】:

标签: php mysql json


【解决方案1】:

我找到了....我创建了一个带有 2 列表的数据库:1° idsensor,2° 传感器值。 我创建了 3 个文件:

connect.php:

<?php

    function Connection(){
    $server="server";
    $user="user";
    $pass="pass";
    $db="database";

    $connection = mysql_connect($server, $user, $pass);

    if (!$connection) {
        die('MySQL ERROR: ' . mysql_error());
    }

    mysql_select_db($db) or die( 'MySQL ERROR: '. mysql_error() );

    return $connection;
}
?>

添加.php:

<?php
include("connect.php");

$link=Connection();

$sensorId=$_POST["sensorId"];
$value=$_POST["value"];

    $query = "INSERT INTO `Log` (`temperature`, `humidity`) 
    VALUES ('".$sensorId."','".$value."')"; 

mysql_query($query,$link);
mysql_close($link);


?>

index.php:

 <?php

    include("connect.php");     

    $link=Connection();

    $result=mysql_query("SELECT * FROM `Log` ORDER BY `timeStamp` DESC",$link);
    ?>

<html>

<?php

    include("connect.php");     

    $link=Connection();

    $result=mysql_query("SELECT * FROM `tempLog

` ORDER BY `timeStamp` DESC",$link);
    ?>

    <html>
       <head>
          <title>Sensor Data</title>
       </head>
    <body>
       <h1>Temperature / moisture sensor readings</h1>

       <table border="1" cellspacing="1" cellpadding="1">
            <tr>
                <td>&nbsp;Timestamp&nbsp;</td>
                <td>&nbsp;Temperature 1&nbsp;</td>
                <td>&nbsp;Moisture 1&nbsp;</td>
            </tr>

          <?php 
              if($result!==FALSE){
                 while($row = mysql_fetch_array($result)) {
                    printf("<tr><td> &nbsp;%s </td><td> &nbsp;%s&nbsp; </td><td> &nbsp;%s&nbsp; </td></tr>", 
                       $row["timeStamp"], $row["temperature"], $row["humidity"]);
                 }
                 mysql_free_result($result);
                 mysql_close();
              }
          ?>

       </table>
    </body>
    </html>



     <table border="1" cellspacing="1" cellpadding="1">
            <tr>
                <td>&nbsp;Sensor;</td>
                <td>&nbsp;value;</td>


            </tr>

      <?php 
          if($result!==FALSE){
             while($row = mysql_fetch_array($result)) {
                printf("<tr><td> &nbsp;%s </td><td> &nbsp;%s&nbsp; </td><td> &nbsp;%s&nbsp; </td></tr>", 
                   $row["timeStamp"], $row["sensorId"], $row["value"]);
             }
             mysql_free_result($result);
             mysql_close();
          }
      ?>

   </table>
</body>
</html>

如果一切正常,任何人都可以说我???

【讨论】:

  • 最好将此代码添加到您的初始问题中。您应该阅读我在how to do databases with PHP 上发布的链接 - 您这样做的方式是使用旧的不受支持的库,该库缺乏功能且不安全。您将通过使用 PDO 创建连接并使用它来存储您的数据来帮助自己。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-02
  • 2019-03-07
  • 1970-01-01
  • 2016-06-25
  • 2021-12-10
  • 1970-01-01
相关资源
最近更新 更多