0

I am building a simple site that gathers some columns from an SQL Server and presenting them in an PHP Datatables site.

I have two different pages that gathers data from the SQL in the same way.

Here is working code:

$sql = "SELECT * FROM DATA_ORDER";
$stmt = sqlsrv_query( $conn, $sql );
if( $stmt === false) {
    die( print_r( sqlsrv_errors(), true) );
}

while( $row_get = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {
      $options .= '<tr><td>' .$row_get['VAARREF']. '</td>'.
             '<td>' .$row_get['VAARREF']. '</td>'.
             '<td>' .$row_get['DOKNR']. '</td>'.
             '<td>' .$row_get['DATUM1']->format('d.m.Y'). '</td>'.
             '<td>' .$row_get['LOKALANM']. '</td>';
            }
    echo $options; 


sqlsrv_free_stmt( $stmt);

No problems there and the date is formatted as it should. DATUM1 column is date column in SQL.

Then I have this page with almost the exact same code:

 $sql = "SELECT * FROM DATA_PAYMENT2";
 $stmt = sqlsrv_query( $conn, $sql );
 if( $stmt === false) {
     die( print_r( sqlsrv_errors(), true) );
 }
 
 while( $row_get = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {
       $options .= '<tr><td> Sen </td>'.
              '<td>' .$row_get['KOPPL_DOK1']. '</td>'.              
              '<td>' .$row_get['SENBETDAT']->format('d.m.Y'). '</td>'.
              '<td>' .$row_get['TOTALT']. '</td>'.
              '<td>' .$row_get['VALUTAKOD']. '</td>'.
              '<td>' .$row_get['DOKTYP']. '</td>'.
              '<td>' .$row_get['EU_KUND']. '</td>'.
              '<td>' .$row_get['ERORDER']. '</td>'.
              '<td>' .$row_get['NAMN']. '</td>'.
              '<td>' .$row_get['Payer_Address']. '</td>'.
              '<td>' .$row_get['SPRAAKKOD']. '</td>'.
              '<td>' .$row_get['ORGNR']. '</td>'.
              '<td>' .$row_get['ERREF']. '</td>'.
              '<td> Totalt </td>'.
              '<td>' .$row_get['DOKNR']. '</td>'.
              '<td>' .$row_get['LOKALANM']. '</td>';
             }
     echo $options;  
 sqlsrv_free_stmt( $stmt);

That recieves an error 500. If I comment out "SENBETDAT" row, then it all works. If I remove the format and the column is empty then it also works, but it wont work when there is data there with the format. I only get blank page. The column is also "date" in SQL and if I change this page to get data from the first table (DATA_ORDER) and get DATUM1, then it also works so I dont think there is any problem with the PHP code, but still I dont have any clue what the problem can be. I have tried to reconfigure the column to "date" but still no work.

Any suggestions?

Dale K
  • 25,246
  • 15
  • 42
  • 71
jp87
  • 1
  • 500 Internal Server Error is a generic error message informing you that the server crashed while processing the request. Beyond that, it's (intentionally) meaningless, and is of very little use for debugging. You need to check the error logs on the server to try and find the underlying exception message. Once you've got that, you stand a chance of identifying the problem. See also [How can I get useful error messages in PHP?](https://stackoverflow.com/questions/845021/how-can-i-get-useful-error-messages-in-php) – ADyson Jul 12 '22 at 22:48
  • But yeah the answer below is likely to have identified the issue anyway - the value you get back from the SQL query will be a string, and you cannot call a function on a string. – ADyson Jul 12 '22 at 22:52
  • You can try `(new DateTime($row_get['SENBETDAT']))->format('d.m.Y')` – Guido Faecke Jul 12 '22 at 23:00
  • Thanks for the answers I think the problem lies in that there are some empty cells and when I try to get the column and format it, it throws an error – jp87 Jul 12 '22 at 23:09

1 Answers1

1

When you get it back from the mysql query it needs to be turned into a DateTime object before you call format(). You can do this:

$dateObj = date_create($row_get['SENBETDAT']);
$dateObj->format('d.m.Y');
Tyler Dill
  • 351
  • 2
  • 6
  • Thanks I have also figure it out that the date column have empty cells so the format won't work, or the format is working but i still get an error because of this. I am trying to see how to get all the data to show even if its empty – jp87 Jul 12 '22 at 22:53