PHP, MSSQL, Date Select

Hi All
I am trying to echo a date held within a MSSQL Table,
the date is being stored as “2019-08-15 16:00:00.000”

Table Columns
id, Expected
4272, 2019-08-15 16:00:00.000

if I run the below select query in SQL Studio manager it works
SELECT Expected FROM [visitor].[dbo].[Visits] WHERE id = 4272;

however running the below in a php web page causes a blank page

$sql = "SELECT Expected FROM [visitor].[dbo].[Visits] WHERE id = 4272";
$stmt = sqlsrv_query( $conn, $sql );
while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {
echo "<tr><td>".$row['Expected']."</td></tr>";
}

IE php will not let me echo the date as it is held within the MSSQL database…

Any thoughts?
Many Thanks
Steve

There are multiple reasons you might be getting a blank page.

To start, what does the ‘view source’ of the blank page show?

Next, is php’s error_reporting set to E_ALL and display errors set to ON, in the php.ini on your system so that php will report and display all errors? For a parse/syntax error, you cannot put these settings into your code since your code never runs to cause the settings to take effect.

Does your code have error handling for the database connection (it has none for the query), so that you would know if the database statements are failing?

Is the posted code actually inside a html table so that the tr/td markup would cause a row to be displayed where you think it should?

Hi… thanks for your help

All other echo statements work and therefore i know that the mssql connection and table tags are ok.

I think it is just the way that the data needs to be echo’d due to it being a mssql datetime, I haven’t however got all your suggested php error display settings turned on…

I’ll give that a try

I personally don’t have an experience with connecting to MSSQL from PHP, but i would give PDO a try, so you may get better error messages like:

$pdo = new PDO('MSSQL_CONNECTION_STRING', 'username', 'password', array(
  PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
));
Sponsor our Newsletter | Privacy Policy | Terms of Service