我使用Microsoft ODBC 11 Driver连接到PHP的MSSQL数据库。我正在成功查询数据库并接收我想要的数据,但显示它时出现问题。我从数据库中获取的数组有一些对象是日期,我不知道如何在表中正确显示它们。现在我收到一个Catchble致命错误:类DateTime的对象无法转换为字符串
这是代码。第二个foreach
中的注释代码是我尝试将对象转换为字符串并仅显示没有对象的其他属性的日期的地方。
<?php
header('Content-type: text/html; charset=utf-8');
require_once('sqlcon.php');
$sql = "SELECT * FROM dbo.operations WHERE OperType=4";
$stmt = sqlsrv_query( $conn, $sql );
if( $stmt === false) {
die( print_r( sqlsrv_errors(), true) );
}
$tableHeaderWritten = false;
echo "<table>";
while($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC) ) {
if(!$tableHeaderWritten) {
echo "<tr>";
foreach ($row as $columns => $rows) {
//var_dump($row);
echo "<th>$columns</th>";
}
echo "</tr>";
$tableHeaderWritten = true;
}
echo "<tr>";
foreach ($row as $columns => $rows) {
if(is_object($rows)) {
//$results = array();
//$results = $rows->format('Y-m-d H:i:s');
//foreach ($rows as $key => $value) {
//var_dump($value);
//echo "<th>$value</th>";
}
}
echo "<th>$rows</th>";
}
echo "</tr>";
}
echo "</table>";
?>
这是我提取的转储数组:
`array (size=23)
'ID' => int 3756022
'OperType' => int 4
'Acct' => int 1
'GoodID' => int 3
'PartnerID' => int 1
'ObjectID' => int 4
'OperatorID' => int 1
'Qtty' => float 0
'Sign' => int 1
'PriceIn' => float 0
'PriceOut' => float 1.98
'VATIn' => float 0
'VATOut' => float 0
'Discount' => float 0
'CurrencyID' => int 1
'CurrencyRate' => float 1
'Date' =>
object(DateTime)[1]
public 'date' => string '2015-05-25 00:00:00' (length=19)
public 'timezone_type' => int 3
public 'timezone' => string 'Europe/Paris' (length=12)
'Lot' => string ' ' (length=1)
'LotID' => int 1
'Note' => string 'Изтриване на период към 25.05.2015' (length=54)
'SrcDocID' => int 1
'UserID' => int 1
'UserRealTime' =>
object(DateTime)[2]
public 'date' => string '2015-05-26 18:12:53' (length=19)
public 'timezone_type' => int 3
public 'timezone' => string 'Europe/Paris' (length=12)`
答案 0 :(得分:1)
感谢Ryan Vincent,我有一个问题的解决方案。我只发布第二个 for(int k = 0; k < length; k++) {
printf("arraynum2 has: %d\n", arraynum2[k]);
/*
if(arraynum1[k] != arraynum2[k]) {
return 0;
}
*/
}
更改的位置。
foreach