我是相當新的PHP和SQL,我試圖拉出一個對象或嵌套對象的值。當我使用print_r命令時,我能夠看到代碼,但我無法將實際日期與主體分開,並且我也無法使用datetime條件搜索sql。我知道「Date」作爲smalldatetime數據類型存儲在sql中,我不知道如何使用它。從嵌套的stdClass對象獲取值,並搜索sql數據類型smalldatetime
當我嘗試使用搜索SQL如下:
$sql = "SELECT * FROM GL_Register WHERE (Register_Id='5' && Date='2010-12-8 0:00:00')";
我收到以下錯誤:
Array ([0] => Array ([0] => 42000 [SQLSTATE] => 42000 [1] => 102 [code] => 102 [2] => [Microsoft][SQL Server Native Client 11.0][SQL Server]Incorrect syntax near '&'. [message] => [Microsoft][SQL Server Native Client 11.0][SQL Server]Incorrect syntax near '&'.))
,所以我不能拉帶日期的查詢中有適當的,因爲我不不知道如何。爲了進一步澄清,我能夠使用以下信息從數據庫中獲取信息,但我仍然無法從日期對象中提取日期。
這裏就是我如何加載對象:
$sql = "SELECT * FROM GL_Register WHERE Register_Id='3'";
$stmt = sqlsrv_query($conn, $sql);
if($stmt === false) {
die(print_r(sqlsrv_errors(), true));
}
else{
echo $sql;
}
while($obj = sqlsrv_fetch_object($stmt)) {
$count++;
$row = '<tr>';
$row .= '<td>'.$count.'</td>';
//$row .= '<td>'.$obj->Date->DateTime->date.'</td>';
$row .= '<td>'.$obj->Account_Id.'</td>';
$row .= '<td>'.$obj->Customer_Id.'</td>';
$row .= '<td>'.$obj->Category_Id.'</td>';
$row .= '<td>'.$obj->Credit_Or_Debit.'</td>';
$row .= '<td>'.$obj->Memo.'</td>';
$row .= '<td>$'.$obj->Amount.'</td>';
$row .= '</tr>';
echo "$row";
}
當我使用 「的print_r($ OBJ)」,這是它顯示:
stdClass Object ([Register_Id] => 3 [Account_Id] => 5 [Register_Number] => 17 [Credit_Or_Debit] => D [Accounting_Period_Id] => 13 [Date] => DateTime Object ([date] => 2010-12-21 00:00:00 [timezone_type] => 3 [timezone] => MST) [Register_Type_Id] => 1 [Reference] => 300002 [Type_CVEO] => C [Customer_Id] => 362 [Vendor_Id] => 1 [Employee_Id] => 1 [Other_Name] => [Offset_Account_Id] => 1 [Splits] => N [Amount] => 179.9600 [Mult_Sign] => 1 [Memo] => Security Services [Status] => O [Is_Offset] => N [Offset_Register_Id] => 4 [Branch_Id] => 2 [Job_Id] => 1 [Service_Ticket_Id] => 1 [Category_Id] => 6 [Job_Expense_Code] => [Expense_Type_Id] => 5 [Post_WIP_Account_Id] => 1 [Primary_Register_Number] => 0 [Part_Id] => 1 [Currency_Id] => 1 [Exchange_Rate] => 1.0000 [Amount_Nat] => 179.9600 [Date_Time_Stamp] => DateTime Object ([date] => 2010-12-21 12:29:06 [timezone_type] => 3 [timezone] => MST))
我需要能夠做兩件事,我覺得我正在撞牆。這只是我無法接觸的,我希望有人能幫助我。
問題:
如何解決使用日期時間標準的SQL查詢(即把所有REGISTER_ID的有內最後3天的日期)?
怎麼拉的日期和時間了對象,並將其放置到一個PHP變量
用'和'代替&& – Orangepill