1

I am trying to get data from sql using php. But I am getting error 500 while retrieving Date values. No error while retrieving other type of data.
Can anyone tell me why it is happening?

Value in cell : "1999/12/31 16:01:05.000"

Windows Server

$tsql = "
    SELECT 
        ProgramName, 
        RepeatID, 
        Round(ScrapFraction*100,2) As Yeld, 
        MachineName, 
        Round(CuttingTime*60,2) AS CuttingTimeMins,
        PostDateTime,
        PostedByUserID, 
        Thickness, 
        Material, 
        PostDateTime 
    FROM 
        dbo.Program";

/* 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));    
}    
echo "<Table width='1200' border='1'>";
    echo "<tr>";
    echo "<Td>Program Name</td>";
    echo "<Td>Repeat</td>";
    echo "<Td>Yeld</td>";
    echo "<Td>MachineName</td>";
    echo "<Td>Cutting Time</td>";
    echo "<Td>PostedBy</td>";
    echo "<Td>Thickness</td>";
    echo "<Td>Material</td>";
    echo "<Td>data</td>";
    echo "</tr>";
while( $row = sqlsrv_fetch_array($stmt))    
{  

    echo "<tr>";
    echo "<Td><a href='xa.php?revice=".$row['ProgramName']."'>".$row['ProgramName']."</a></td>";
    echo "<Td><center>".$row['RepeatID']."</center></td>";
    echo "<Td><center>".$row['Yeld']."%</center></td>";
    echo "<Td>".$row['MachineName']."</td>";
    echo "<Td>".$row['CuttingTimeMins']."</td>";
    echo "<Td><center>".$row['PostedByUserID']."</td>";
    echo "<Td><center>".$row['Thickness']."</center></td>";
    echo "<Td><center>".$row['Material']."</center></td>";
    echo "</tr>";
}
/* Free statement and connection resources. */    
sqlsrv_free_stmt( $stmt);    
sqlsrv_close( $conn);
Binil Anto
  • 97
  • 1
  • 11
Dashiphone
  • 59
  • 1
  • 9
  • 4
    Please add the db-table schema (columns and column types), example data in the table, expected output and what output you're currently getting. Also, if you get a 500 error message, you should check your servers error log for the full error message (or enable `display_errors` in your php.ini file while developing). – M. Eriksson Aug 12 '19 at 11:25
  • The only possible `datetime` column is `PostDateTime` (and it's twice in the statement). But I can't see the output from this column - you never echo `$row["PostDateTime"]` for example. Why do you think, that `datetime` columns are the reason for your error? Thanks. – Zhorov Aug 12 '19 at 11:44
  • This same query has works in SQL. But, when i'm try get this same data from this same query i have a error. echo "
    ".$row['PostDateTime']."
    "; When I'm put this line i see error 500. In php.ini im set the error at ON. Is IIS server, i don't know how to checked error com
    – Dashiphone Aug 12 '19 at 11:49
  • @Dashiphone OK, what is the error message? – Zhorov Aug 12 '19 at 11:50
  • Recoverable fatal error: Object of class DateTime could not be converted to string in C:\intranet\x.php on line 53 – Dashiphone Aug 12 '19 at 11:59
  • @Dashiphone This is one solution: https://stackoverflow.com/questions/57392400/my-table-has-two-timestamp-values-and-remaining-are-string-value-i-want-to-disp/57392741#57392741. Just place `"ReturnDatesAsStrings" => true` in the connection options. – Zhorov Aug 12 '19 at 12:01
  • Why fight against the extension features? Just call [DateTime::format()](https://www.php.net/manual/en/datetime.format.php) with the format of your choice. – Álvaro González Aug 12 '19 at 15:36

0 Answers0