根据数据库

时间:2015-06-07 09:23:58

标签: php mysql

我的下面的PHP是从数据库获取当前数据的数据(如果存在于Sql表中使用时区日期和id来选择当前日期的日期。

但;

我希望它选择当前周数据,如果今天是星期日它应该从星期一到今天获取数据,如果表中存在针对该ID的数据

我的数据库表如下

        Date                Id                Data to be fetched

2014-08-29 00:00:00       12345                       ABC
2014-08-29 00:00:00       12345                       ABC
2014-08-30 00:00:00       12345                       ABC
2014-08-31 00:00:00       12345                       ABC
2014-08-31 00:00:00       12345                       ABC
2014-08-02 00:00:00       12345                       ABC
2014-08-02 00:00:00       12345                       ABC
2014-08-02 00:00:00       12345                       ABC
2014-08-03 00:00:00       12345                       ABC
2014-08-03 00:00:00       12345                       ABC
2014-08-04 00:00:00       12345                       ABC
2014-08-04 00:00:00       12345                       ABC

PHP:代码

<?php

  // This is used to select Time Zone
date_default_timezone_set("Indian/Chagos");
        $date= date("y-m-d 00:00:00");

        //Databse Connection
                $mysqli=mysqli_connect('localhost','root','password','db');
    //Data From Ajax For Id to select data against that id

            $id= $_POST['storedValue'];
                //Sql Query
                                                                //Select data against date and PK Id
        $query ="SELECT * FROM master WHERE  StudentRegID='$id' And AttendanceDate='$date' ";
$result = mysqli_query($mysqli,$query)or die(mysqli_error());
$num_row = mysqli_num_rows($result);
            while($row=mysqli_fetch_array($result))
    {
            //Echo result
      echo "{ y: ".$row[9]." ,label: '".$row[6]."'},";  
    }
    mysqli_close($mysqli);

?>

它不起作用:

你能说出我错在哪里吗?

    $id= $_POST['storedValue'];
        $query ="SELECT * FROM master WHERE  StudentRegID='$id' AND DATEDIFF(NOW(), Date) < 7 ";
$result = mysqli_query($mysqli,$query)or die(mysqli_error());
$num_row = mysqli_num_rows($result);
            while($row=mysqli_fetch_array($result))
    {



      echo "{ y: ".$row[9]." ,label: '".$row[6]."'},";




    }

1 个答案:

答案 0 :(得分:0)

您可以在DATEDIFF列与当前日期之间使用Date,并仅检索差异小于7的行,如:

SELECT * [...]
WHERE  StudentRegID='$id' AND DATEDIFF(NOW(), Date) < 7
-- if you want only the lines where something happened today :
AND DATE(AttendanceDate) = DATE('$date') 
;