gpt4 book ai didi

php - 使用 MySQLI 将 DateDiff 列添加到 PHP 表

转载 作者:行者123 更新时间:2023-11-28 23:45:59 25 4
gpt4 key购买 nike

我正在尝试向我的数据库表中添加一列,以显示时间戳和当前日期之间的差异。我试过使用 DateDiff 创建另一个查询,但我不确定我做错了什么。谁能帮忙?

我的代码如下。

    <?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 添加到原始查询中。您还需要使用 fetch_assoc 而不是 fetch_array,以便您可以引用 $row 中的字段名称。

$result = mysqli_query($con,"SELECT *, DATEDIFF(CURDATE, dAdded) as dDate FROM pokemon ORDER BY stats");
while($row = mysqli_fetch_assoc($result))
{

}

关于php - 使用 MySQLI 将 DateDiff 列添加到 PHP 表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33573934/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com