【发布时间】:2017-08-26 01:25:47
【问题描述】:
嘿,我有一个需要定期填充传感器值的在线数据库。有 7 个变量(列)加上一个时间戳列来构成表格的每一行。问题是这些变量一次推送几个,其余为0。现在我正在编写一个算法,如果最新行的所有值(按时间戳)都被完全填充(不是0),它只会插入一行,否则它将更新最新的行。 这是我的插入 php 文件之一的 PHP 代码,它一次插入 3 个变量 - 温度、压力和高度。
<?php
// Prepare variables for database connection
$dbusername = "uxpertla_terr"; // enter database username, I used "arduino" in step 2.2
$dbpassword = "abc123"; // enter database password, I used "arduinotest" in step 2.2
$server = "localhost";
// Connect to your database
$dbconnect = mysqli_connect($server, $dbusername, $dbpassword);
// Prepare the SQL statement
$sdl = "SELECT * FROM uxpertla_test_db.testt ORDER BY time DESC LIMIT 1";
$snd=mysqli_query($sdl);
$row = mysqli_fetch_array($snd,MYSQLI_ASSOC);
if($row)
{
if($row['Temperature']==0||$row['Pressure']==0||$row['Humidity']==0||$row['Light']==0)
{
$sql = "UPDATE uxpertla_test_db.testt
SET temp='".$_GET["value"]."',
pressure='".$_GET["value2"]."',
altitude='".$_GET["value3"]."'
WHERE time=".strtotime($row['time'])."";
}
else
$sql = "INSERT INTO uxpertla_test_db.testt
(temp,pressure,altitude) VALUES('".$_GET["value"]."', '".$_GET["value2"]."','".$_GET["value3"]."')";
}
else
$sql = "INSERT INTO uxpertla_test_db.testt
(temp,pressure,altitude) VALUES('".$_GET["value"]."', '".$_GET["value2"]."','".$_GET["value3"]."')";
// Execute SQL statement
mysqli_query($sql);
?>
对不起,如果代码不漂亮,但我需要关于第二个 if 块中更新查询的 where 子句的帮助..
【问题讨论】:
-
如果你的目标是一个时间戳,比如在一个设定的时间段内,你可以使用 SQL Between 关键字。其中 timeValue 在“timeOne”和“timeTwo”之间。对空闲的手感到抱歉
-
您正在使用未维护、不安全、已弃用的数据库函数。十多年来,PHP 一直在鼓励使用替代品。停下来! stackoverflow.com/q/12859942/1255289
-
我知道不推荐使用,但所有 mysql 功能都可以工作。他们不是问题。问题是更新语句的 where 子句。请帮我解决那条线..
-
mysql_error()在查询/查询中返回什么?和php的报错php.net/manual/en/function.error-reporting.php -
错误日志没有显示任何内容...