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

标签 php mysql mysqli datediff

我正在尝试向我的数据库表中添加一列,以显示时间戳和当前日期之间的差异。我试过使用 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/

相关文章:

php - 无法弄清楚如何将 &lt;input type ="checkbox"> 转换为 PHP bool 值

php - 如何显示查询结果

php - php和mysql中最大数量的目的

php - 使用连接从三个表中获取结果,其中一个表包含多个结果

php - 带有 order by rand 函数的 where 语句不起作用

php - C++ WinSock 从 PHP 页面获取数据?

php - 对于巨大的表格数据,使用 div 标签是否比使用 table tr 和 td 标签更快?

php - file_exists 或 getimagesize 仅适用于本地绝对文件路径,但不适用于 PHP 中的 URL

mysql - 获取同一日期的数据总和并获取每周报告的数组

php - 为什么我在帖子中输入内容时我的博客帖子条目是空的?