简体   繁体   English

使用PHP在SQL中选择两个日期之间的行

[英]Selecting rows between two dates in SQL with PHP

I'm trying to get rows from my database between two dates. 我正在尝试从两个日期之间的数据库中获取行。 I've used other questions here to get to this point, but I don't know how to move forward from here. 为了解决这一点,我在这里使用了其他问题,但是我不知道如何从这里前进。

Here is the code, and underneath are the screenshots of the output and a photo running the query in SQL. 这是代码,下面是输出的屏幕截图和运行SQL查询的照片。

Thanks for the help in advance! 我在这里先向您的帮助表示感谢!

-- -

This first section of code outputs the ID, name, etc. 代码的第一部分输出ID,名称等。

<?php

$sql = "SELECT * FROM songs ";
$result = $link->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "id: " . $row["ID"] . " - Name: " . $row["name"] . " " . $row["released"]. "<br>";
}
} else {
    echo "0 results";
}

/* close connection */    

$link->close();

?>

This section of code outputs "0 Results" 此部分代码输出“ 0个结果”

<?php

$from_date = '2015-01-01';
$to_date = '2017-04-04';

$sql = "SELECT * FROM songs WHERE released BETWEEN '" . $from_date .  "' AND '" . $to_date . "' ";
$result = $link->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "id: " . $row["ID"] . " - Name: " . $row["name"] . " " . $row["released"]. "<br>";
    }
} else {
    echo "0 results";
}

/* close connection */    

$link->close();

?>

This is what the page looks like with the above code. 上面的代码就是页面的样子。

I've successfully selected the rows in SQL. 我已经成功选择了SQL中的行。

EDIT: I made a mistake when writing the code into the question for the IF ELSE statement on the 2nd block of code. 编辑:将代码写入第二块代码中的IF ELSE语句的问题时,我犯了一个错误。 I just updated it to what I actually have on the site. 我只是将其更新为网站上的实际内容。 I didn't change anything based on the solutions, and am still not getting the rows to print. 我没有根据解决方案进行任何更改,而且仍然无法打印行。

Your results are listed - as you can see from your screenshot. 您的结果将列出-从屏幕截图中可以看到。 Problem: While along with else does not make sence. 问题: While没有else意义。

your last example is missing a } so it says: while { } else { echo "0 results"} 您的最后一个示例缺少}因此它说: while { } else { echo "0 results"}

But the echo "0 results" should be related to the num_rows -check, not to the while . 但是echo "0 results"应该与num_rows -check相关,而不与while

You done everything right, except using while/else ; 除了使用while/else ,您一切都做对了; Your code (2-nd) section should look like this: 您的代码(第2个)部分应如下所示:

if ($result->num_rows > 0) {

  while () {
     //print your rows
  }
} else {

  //Say that there's 0 results
}

Ah man, I just needed to had to comment out this line from the first block: 啊,我只需要在第一行中注释掉这一行:

$link->close();

I apologize to everyone who answered if it wasn't clear that the blocks of code followed one another. 我向所有回答的人表示歉意,如果不清楚这些代码块是否彼此跟随。 Anyways, for anyone else with the same problem, don't close the connection -_- 无论如何,对于其他有相同问题的人,请不要关闭连接-_-

Here's what is output when I comment out the line from the first block 这是当我注释掉第一个块中的行时输出的内容

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM