我有int
数据库 t_calibrator 中的数据(校准周期 = 12、24 和 36)月份,我想用 $callast
中的输入日期进行计算,
这是我创建的代码(但总是得到 1970-01-01):
$calperiod = mysqli_query($conn, "SELECT calperiod FROM t_calibrator WHERE id_calibrator='$id_calibrator'");
$add = date('Y-m-d', strtotime('+".$calperiod" month', strtotime($callast)));
我的引用代码来自$add = date('Y-m-d', strtotime('+24 month', strtotime($callast)));
是的,这有效,但只能持续 24 个月。
更新 现在我尝试使用这段代码:
$sql = "SELECT * FROM t_calibrator WHERE id_calibrator = '$id_calibrator'";
$result = mysqli_query ($conn,$sql);
$caldata = mysqli_fetch_array ($result);
$add = date('Y-m-d', strtotime('+".$caldata['calperiod']." months', strtotime($callast)));
但是显示这样的错误:
Parse error: syntax error, unexpected 'calperiod' (T_STRING), expecting ',' or ')' in /storage/h7/747/1148747/public_html/pages/calinputhistory.php on line 19
有人可以帮我解决这个麻烦吗? (抱歉我的英语不好)
最佳答案
mysqli_query
的返回结果要么是结果集(如果查询执行成功),要么是 FALSE(如果发生错误)。测试 mysqli_query 的返回结果,如果不为 FALSE,则获取一行。如果我们有一行,请从 calperiod 列中获取值。
$sql = "SELECT calperiod FROM ... ORDER BY ... LIMIT 1";
if( $sth = mysqli_query($conn,$sql) ) {
if( $row = mysqli_fetch_assoc($sth) ) {
$calperiod = $row["calperiod"];
echo $calperiod;
// we have a value ...
} else {
echo "no row returned";
}
} else {
echo "error in query execution";
die(mysqli_error($conn);
}
关于PHP:如何从数据库中添加日期和月份?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42967889/