$selectResitDate = "SELECT startdate FROM resit";
$runselectResitDate = mysqli_query($conn, $selectResitDate);
if (!$runselectResitDate) {
die('Invalid query: ' . mysql_error());
}
while($row = mysqli_fetch_assoc($runselectResitDate))
{
$datetime2 = new DateTime($row=['startdate']);
$interval = $datetime1->diff($datetime2);
$diff=$interval->format('%a');
echo $diff;
}
PLZ帮助,它只是不起作用..显示没有结果 我已多次看到这个问题了,有人能告诉我为什么会出现这个问题,以及如何避免这个问题?
答案 0 :(得分:4)
删除等号:
lshw
它应该是:
$datetime2 = new DateTime($row=['startdate']);
^
否则,您只需为$datetime2 = new DateTime($row['startdate']);
分配单个元素数组,并将其用作$row
函数的参数,而不是获取值。
另外,将Datetime
更改为mysql_error()
。
答案 1 :(得分:1)
=
导致问题。当我们通过密钥访问数组值时,我们使用$array['key']
。所以使用
:
$datetime2 = new DateTime($row['startdate']);
而不是:
$datetime2 = new DateTime($row=['startdate']);
答案 2 :(得分:-1)
变化:
$datetime2 = new DateTime($row=['startdate']);
致:
$datetime2 = new DateTime($row['startdate']);
或者您也可以尝试此代码
$conn = mysql_connect("host", "user", "password");
if (!$conn) {
echo "Unable to connect to DB: " . mysql_error();
exit;
}
if (!mysql_select_db("yourdbname")) {
echo "Unable to select mydbname: " . mysql_error();
exit;
}
$sql = "SELECT startdate FROM resit";
$result = mysql_query($sql);
while ($row = mysql_fetch_assoc($result)) {
$datetime2 = new DateTime($row['startdate']);
$interval = $datetime1->diff($datetime2);
$diff=$interval->format('%a');
echo $diff;
}