<?php
$servername = "localhost";//数据库地址
$username = "root";//数据库用户
$password = "11223344";//数据库密码
$dbname = "historical_data";//链接的数据库名字
try {
$conn = new PDO("mysql:host=$servername;port=3306;dbname=$dbname", $username, $password);//链接数据库
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // 设置 PDO 错误模式为异常
echo "PDO的API连接成功";
$conn=null;//关闭
}
catch(PDOException $e){
echo "errorCode:DataBase".$e->getMessage();
}
?>
在浏览器上测试下, ip地址填写自己的服务器地址
http://ip地址/php_pdo.php
如果用户按照前面的章节,自己的数据库里面应该有设备的数据了
现在数据库里面已经存储了设备的数据了,获取某个设备某个时间段的历史数据格式规定如下:
http://ip地址/php_mysql_get.php?client_id=设备的ID&data_time_start=开始时间&data_time_stop=结束时间
假设获取client_id为863488051205089设备在2022-07-30 00:00:00 到 2022-07-30 22:48:00 的历史数据
http://ip地址/php_mysql_get.php?client_id=863488051205089&data_time_start=2022-07-30 00:00:00&data_time_stop=2022-07-30 22:48:00
数据返回为json格式规定如下:
{
"code": "OK",
"client_id": "设备的client_id",
"data": [
{
"data_time": "2022-07-30 22:45:08",
"temperature": 34,
"humidity": 40
},
{
"data_time": "2022-07-30 22:45:18",
"temperature": 34,
"humidity": 40
},
{
"data_time": "2022-07-30 22:45:28",
"temperature": 34,
"humidity": 40
}
]
}
<?php
//http://ip地址/php_mysql_get.php?client_id=863488051205089&data_time_start=2022-07-30 00:00:00&data_time_stop=2022-07-30 22:48:00
$servername = "localhost";//数据库地址
$username = "root";//数据库用户
$password = "11223344";//数据库密码
$dbname = "historical_data";//链接的数据库名字
try {
$client_id = $_GET["client_id"];
$data_time_start = $_GET["data_time_start"];
$data_time_stop = $_GET["data_time_stop"];
try {
$conn = new PDO("mysql:host=$servername;port=3306;dbname=$dbname", $username, $password);//链接数据库
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // 设置 PDO 错误模式为异常
$sql = "select data_time,temperature,humidity from humiture_table where client_id="."'".$client_id."'"."and data_time>="."'".$data_time_start."'". "and data_time<="."'".$data_time_stop."'";
$result=$conn->prepare($sql);//准备查询语句
$result->execute(); //执行查询语句,并返回结果集
//$result = $conn->query($sql);//发送SQL语句并接收数据库返回
$note;$i=0; //初始化变量
if ($result && $result->rowCount()) {//查询到数据
while($row = $result->fetch()) {//循环读出数据
$note["data_time"]=$row['data_time'];
$note["temperature"]=$row['temperature'];
$note["humidity"]=$row['humidity'];
//放到二维数组里
$notes[$i++]=$note;
//echo $row['temperature'].$row['humidity']. "\r\n";//打印数据
}
$notes = array('code'=>'OK','client_id'=>$client_id,'data'=>$notes);//组合成key-value形式的数组
echo json_encode($notes);//把数组转为json格式
}
else{
echo "{\"code\":\"ERR\",\"msg\":\"no data\"}";
}
$conn=null;//关闭
}
catch(PDOException $e){
$err = array('code'=>'ERR','msg'=>'database execute','info'=>$e->getMessage());
echo json_encode($err);//把数组转为json格式
}
} catch (Exception $e) {
echo $e->getMessage();
}
?>
在浏览器上测试下, ip地址填写自己的服务器地址,设备id和查询的时间段按照自己数据库里面记录的数据填写(保证可以查到数据)
http://ip地址/php_mysql_get.php?client_id=设备的client_id & data_time_start=2022-07-30 00:00:00&data_time_stop=2022-07-30 22:48:00
然后应该就可以了
由于get接口的请求数据会在http://上显示出来,所以最后是做成post接口的.
在网站根目录建一个后缀名为.php的文件,里面的内容按照下面的填写
<?php
//http://ip地址/php_mysql_post.php
//client_id=863488051205089&data_time_start=2022-07-30 00:00:00&data_time_stop=2022-07-30 22:48:00
$servername = "localhost";//数据库地址
$username = "root";//数据库用户
$password = "11223344";//数据库密码
$dbname = "historical_data";//链接的数据库名字
try {
$client_id = $_POST["client_id"];
$data_time_start = $_POST["data_time_start"];
$data_time_stop = $_POST["data_time_stop"];
try {
$conn = new PDO("mysql:host=$servername;port=3306;dbname=$dbname", $username, $password);//链接数据库
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // 设置 PDO 错误模式为异常
$sql = "select data_time,temperature,humidity from humiture_table where client_id="."'".$client_id."'"."and data_time>="."'".$data_time_start."'". "and data_time<="."'".$data_time_stop."'";
$result=$conn->prepare($sql);//准备查询语句
$result->execute(); //执行查询语句,并返回结果集
//$result = $conn->query($sql);//发送SQL语句并接收数据库返回
$note;$i=0; //初始化变量
if ($result && $result->rowCount()) {//查询到数据
while($row = $result->fetch()) {//循环读出数据
$note["data_time"]=$row['data_time'];
$note["temperature"]=$row['temperature'];
$note["humidity"]=$row['humidity'];
//放到二维数组里
$notes[$i++]=$note;
//echo $row['temperature'].$row['humidity']. "\r\n";//打印数据
}
$notes = array('code'=>'OK','client_id'=>$client_id,'data'=>$notes);//组合成key-value形式的数组
echo json_encode($notes);//把数组转为json格式
}
else{
echo "{\"code\":\"ERR\",\"msg\":\"no data\"}";
}
$conn=null;//关闭
}
catch(PDOException $e){
$err = array('code'=>'ERR','msg'=>'database execute','info'=>$e->getMessage());
echo json_encode($err);//把数组转为json格式
}
} catch (Exception $e) {
echo $e->getMessage();
}
?>
打开测试工具 网站 http://www.ecjson.com/httpurl/
通信方式选择 POST
路径: http://ip地址/php_mysql_post.php