使用MySQLI向PHP表中添加DateDiff列
我试图在数据库表中添加一列,显示时间戳和当前日期之间的差异。我尝试使用DateDiff创建另一个查询,但我不确定我到底做错了什么。 有人能帮忙吗 我的代码如下使用MySQLI向PHP表中添加DateDiff列,php,mysql,mysqli,datediff,Php,Mysql,Mysqli,Datediff,我试图在数据库表中添加一列,显示时间戳和当前日期之间的差异。我尝试使用DateDiff创建另一个查询,但我不确定我到底做错了什么。 有人能帮忙吗 我的代码如下 <?php $result = mysqli_query($con,"SELECT * FROM pokemon ORDER BY stats"); echo "<table border='1'> <tr>
<?php
$result = mysqli_query($con,"SELECT * FROM pokemon ORDER BY stats");
echo "<table border='1'>
<tr>
<th>Pokemon Name</th>
<th>Type One</th>
<th>Type Two</th>
<th>Move One</th>
<th>Move Two</th>
<th>Move Three</th>
<th>Move Four</th>
<th>Stats</th>
<th>ID</th>
<th>Date Added</th>
<th>Days on File</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
$query = "SELECT DATEDIFF(CURDATE, $row[dAdded])FROM pokemon AS days";
$date = mysqli_query($con, $query);
echo "<tr>";
echo "<td>" . $row['pName'] . "</td>";
echo "<td>" . $row['type1'] . "</td>";
echo "<td>" . $row['type2'] . "</td>";
echo "<td>" . $row['move1'] . "</td>";
echo "<td>" . $row['move2'] . "</td>";
echo "<td>" . $row['move3'] . "</td>";
echo "<td>" . $row['move4'] . "</td>";
echo "<td>" . $row['stats'] . "</td>";
echo "<td>" . $row['ID'] . "</td>";
echo "<td>" . $row['dAdded'] . "</td>";
echo "<td>" . $date . "days</td>";
echo "</tr>";
}
echo "</table>";
?>
我怀疑问题出在您的
DATEDIFF()
函数中,更具体地说是在$row[dAdded]
中。这不应该是带引号的$row['dAdded']
?而且我相信CURDATE
需要像CURDATE()
这样的括号
您可以将datediff添加到原始查询中。您还需要使用fetch_assoc而不是fetch_数组,以便可以引用$row中的字段名
$result = mysqli_query($con,"SELECT *, DATEDIFF(CURDATE, dAdded) as dDate FROM pokemon ORDER BY stats");
while($row = mysqli_fetch_assoc($result))
{
}
这是一个很好的添加,开销更少:)
$result = mysqli_query($con,"SELECT *, DATEDIFF(CURDATE, dAdded) as dDate FROM pokemon ORDER BY stats");
while($row = mysqli_fetch_assoc($result))
{
}