我有一个MSSQL数据库,我希望使用php在显示屏上写日期数据(列名Zeitstempel
)。但我得到了这个错误:
捕获致命错误:类DateTime的对象无法转换为字符串
为什么我会收到这样的错误?
date_default_timezone_set(Europe/Berlin) ;
$connectionInfo = array( "UID"=>$uid,
"PWD"=>$pwd,
"Database"=>$databaseName);
/* Connect using SQL Server Authentication. */
$conn = sqlsrv_connect( $serverName, $connectionInfo);
$tsql = "SELECT Zeitstempel FROM statistik where mpid=1 ";
/* Execute the query. */
$stmt = sqlsrv_query( $conn, $tsql);
if ( $stmt )
{
echo "Statement executed.<br>\n";
}
else
{
echo "Error in statement execution.\n";
die( print_r( sqlsrv_errors(), true));
}
/* Iterate through the result set printing a row of data upon each iteration.*/
while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_NUMERIC))
{
echo "Col1: ".$row[0]."\n";
}
答案 0 :(得分:1)
这是因为sqlserver date
类型转换为php DateTime
类型
http://msdn.microsoft.com/en-us/library/cc296193.aspx
使用format
方法$row[0]->format('...')
答案 1 :(得分:-1)
试试这项工作
while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_NUMERIC))
{
echo "Col1: ".$row[0]->format('Y-m-d H:i:s')."\n";
}