通过特定月份和/或年份选择行

时间:2018-03-23 10:49:54

标签: php mysqli

我正在使用这个PHP / MySQL代码来使用特定的月份或数年从我的数据库中获取行,但它似乎只获得1行而不是多行,或者在某些情况下根本不会选择任何行在我设置的日期,即使数据库中存在这些日期:

$year = 2017;

$month = 11;

$month2 = $month + 1;

$sql_graphmonth = "SELECT jobTOTAL FROM jobcards WHERE jobcards.date BETWEEN '$year-$month-01' AND '$year-$month2-01'";

$query_graphmonth = mysqli_query($conn, $sql_graphmonth) or die(mysqli_error($conn));

$row_graphmonth = mysqli_fetch_assoc($query_graphmonth);

数据库中的列是日期列,由current_timestamp创建,因此它采用以下格式:2018-03-14 09:15:51

2 个答案:

答案 0 :(得分:2)

您只需拨打mysqli_fetch_assoc()一次,因此您只需获得第一行。您需要在循环中调用它以获取所有行,您可以将其放入数组中:

$all_graphmonth = array();
while ($row = mysqli_fetch_assoc($query_graphmonth)) {
    $all_graphmonth[] = $row;
}

答案 1 :(得分:0)

Additonaly,您应该为查询添加时间,如

 ... "SELECT jobTOTAL FROM jobcards WHERE jobcards.date BETWEEN '$year-$month-01 00:00:00' AND '$year-$month2-01 23:59:59'";