表单提交后,我将返回一些搜索结果。一切正常,直到我到达一个名为SMALLDATETIME并允许为NULL的字段。返回的行中至少有一个字段为NULL。正如您在下面的代码中看到的那样,当它不为NULL时,我会将其从Date转换为String没问题,但是对于包含NULL的行,我收到一条错误消息。
有任何想法吗?
代码:
$search_results = sqlsrv_query($database_connection, $search_results_sql, array(), array( "Scrollable" => SQLSRV_CURSOR_KEYSET )); if($search_results){ $returned_rows = sqlsrv_has_rows($search_results); if($returned_rows === true){ getAssetSearchData($search_results); } } function getAssetSearchData($search_results){ while($search_results_option = sqlsrv_fetch_object($search_results)){ echo " <tr> <td class='col45'>".date_format($search_results_option->HardwareAssetLastUpdateTime,"d/m/Y H:i")."</td> <td class='col46'>".$search_results_option->HardwareAssetLastUpdatedByName."</td> <td class='col47'>".date_format($search_results_option->HardwareAssetLastDiscoveryScanDate,"d/m/Y H:i")."</td></tr>"; } }
错误信息:
警告:date_format()期望参数1为DateTimeInterface,在中为null
<td>".( is_null($search_results_option->HardwareAssetLastDiscoveryScanDate) ? '' : date_format($search_results_option->HardwareAssetLastDiscoveryScanDate, "d/m/Y H:i") ) ."</td>";