【问题标题】:Hoe to set odbc dateformat in query如何在查询中设置 odbc 日期格式
【发布时间】:2018-01-23 08:38:13
【问题描述】:

我已从 Ms Access 数据库获取数据为 PHP JSON 格式。我在查询中有通过日期参数。数据库数据类型是日期/时间。查询 mm/dd/yyyy 中需要日期格式。

我已经给出了结果

ConnectedResource id #4{"status":0,"0":[]}

print_r($stmt)

结果资源 id #4 和 goose else 语句。

<?php

    $reg = $_GET['reg'];
    $fdate = $_GET['fdate'];
    $tdate = $_GET['tdate'];
    $yid = $_GET['yid'];


    $sql = "select a.RegNo,c.Standard,b.RollNo,d.Division,b.Std_Name as StudentName,AttendanceDate,Attendance_Type,Remark 
                                         from (((Attendance_mas as a 
                                         inner join Std_Reg as b on a.RegNo = b.RegNo) 
                                         inner join StandardMaster as c on a.Standard = c.stdid) 
                                         inner join DivisionMaster as d on a.Division =d.DivisionID) 
                                         where a.RegNo= $reg and a.AttendanceDate between ($fdate) and 
                                         ($tdate1) and a.yearid = $yid Order by AttendanceDate desc";
    //$sql = "select * from Std_Reg";

    $stmt = odbc_exec($conn, $sql);

print_r($stmt);
$result = [];
do {
    while ($row = odbc_fetch_array($stmt)){
       $result[] = $row; 
    }
} while (odbc_next_result($stmt));

if(count($result)>0)
{
    $result1['status']=1;//"Login successfully";
    array_push($result1,$result);
}
else
{
     //$result[]="null";
    $result1['status']=0;//"Record not found";
    array_push($result1,$result);
}
odbc_free_result($stmt);
odbc_close($conn); //Close the connection first

echo json_encode($result1); //You will get the encoded array variable

?>

【问题讨论】:

  • 我不明白这个问题,您是否正在寻找一种方法来格式化您的 date 或将您的 date 传递给查询?
  • $fdate 和 $tdate 传入查询其需要的格式 MM/dd/YYYY。

标签: php database odbc ms-access-2007


【解决方案1】:

我建议使用PDO PREPARED STATEMENT 将参数绑定到您的查询:

$reg = $_GET['reg'];
$fdate = date("m/d/Y", strtotime($_GET['fdate']));//THis will format the date
$tdate = date("m/d/Y", strtotime($_GET['tdate']));
$yid = $_GET['yid'];

$sql = "select a.RegNo,c.Standard,b.RollNo,d.Division,b.Std_Name as StudentName,AttendanceDate,Attendance_Type,Remark 
                                     from (((Attendance_mas as a 
                                     inner join Std_Reg as b on a.RegNo = b.RegNo) 
                                     inner join StandardMaster as c on a.Standard = c.stdid) 
                                     inner join DivisionMaster as d on a.Division =d.DivisionID) 
                                     where a.RegNo= ? and a.AttendanceDate between (?) and 
                                     (?) and a.yearid = ? Order by AttendanceDate desc";

$res = odbc_prepare($conn, $sql);
if(!$res) die("could not prepare statement ".$sql);
$parameters = array($reg,$fdate,$tdate,$yid);
if(odbc_execute($res, $parameters)) {
   //Fetch the result
} else {
  // handle error
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-22
    • 1970-01-01
    • 2015-03-05
    • 2023-01-21
    • 1970-01-01
    • 1970-01-01
    • 2022-08-03
    • 2011-10-22
    相关资源
    最近更新 更多