php - MySQL 语法不返回结果

标签 php mysql datetime date-format

我的数据库中有此列(已创建),它具有 datetime 属性。我想使用超链接中的 $_GET[''] 获取同一年份和同一月份括号内的记录行。

链接与标题相呼应的是上一页中的 $datetime = strtotime($row['Created']);

 $datetime = strtotime($row['Created']);
 $tday = date("F, Y", $datetime);
 <a href="news.php?year='.$datetime.'">'.$tday.'</a>
//It returns this for example 1437476308

在我的 fetch_array 语句中

if(isset($_GET['year'])){
  $year= mysqli_real_escape_string($connection, strip_tags($_GET['year']));
//For example $year ends up with 1437476308
SELECT * 
  FROM Posts 
  WHERE DATE_FORMAT(Created, '%Y-%m')=DATE_FORMAT({$year}, '%Y-%m');

查询应返回 2 行,但返回 0 行。我使用了 MYSQL 控制台,我得到了相同的 0 行,有一些地方做得不对。

最佳答案

我会更改你的代码

$dateTime = DateTime::createFromFormat('Y-m-d H:i:s', $row['Created']);

echo '<a href="news.php?date=' . $dateTime->format('Y-m-01') . '">' . $dateTime->format('F, Y') . '</a>';

查询将是:

if(isset($_GET['date'])){
    $firstDay = mysqli_real_escape_string($connection, $_GET['date']);

    $query = "SELECT * 
              FROM projects 
              WHERE Date BETWEEN '$firstDay' AND LAST_DAY('$firstDay')";
}

另外,不要忘记向 projects 表中的 Date 列添加索引

关于php - MySQL 语法不返回结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31940075/

相关文章:

php - PHP 有 struct 数据类型吗?

c# - 如何将此查询转换为不等于版本

python - PySpark:将时间戳转换为数据帧中的整数时不一致

android - 将日期时间从 Android 发送到 MySQL

javascript - 如何获取回车键按下事件中的输入值?

php - 从 PHP 中具有动态属性名称的类获取 const 属性

php - wp_register_style() 和 wp_enqueue_style 的区别

javascript - 使用 Node.js 中的删除请求动态从数据库中删除数据

php - 使用 PHPWord 动态表行

javascript - Moment.js isBefore() 在检查中午 12 点/中午之前是否工作时不起作用