用于选择日期的MYSQL SELECT语句

时间:2016-07-29 07:11:07

标签: php mysql date

我正在尝试使用语句从MYSQL数据库中检索数据,但由于某些原因,该语句未按预期工作。可能是什么原因?值是表单t1和t2的日期输入。 2005-04-06形式的日期值。它可能是日期形式问题吗? ...尽管数据库中有数据,但我得到“没有显示联系人”。这是语法错误吗?

<?php
session_start();

$servername = "localhost";
$username = "root";
$password = "ub435!";
$dbname = "funtest";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
$_SESSION['post-data'] = $_POST;

$t1 = $_SESSION['post-data']['t1'];
$t2 = $_SESSION['post-data']['t2'];
$time1 = mysqli_real_escape_string($conn, $t1);
$time2 = mysqli_real_escape_string($conn, $t2); 


$sql =  "SELECT DISTINCT msisdn FROM customer WHERE DATE_FORMAT(time_paid, '%Y-%c-%e') BETWEEN ADDDATE('$time1',INTERVAL 0 HOUR) AND ADDDATE('$time2',INTERVAL '23:59' HOUR_MINUTE)";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
  echo "Number of Recipients: ";  echo "$result->num_rows <br> <br>";
    // output data of each row
    while($row = $result->fetch_assoc()) {
 $mobilenumber = $row['msisdn'];
      echo "Mobile : " . "$mobilenumber" . "<br>";

    }
} else {
    echo "No Contacts to Display";
}

$conn->close(); 
?>

1 个答案:

答案 0 :(得分:1)

您错过了$

$sql =  "SELECT DISTINCT msisdn FROM customer where time_paid  BETWEEN '$time1' AND '$time2')";