I have a table in and MSSQL DB that list period and week start dates for any date in a financial year but the page is only display the week numbers and period number none of the dates does any body have any ideas please.

<?php

  $serverName = 'DESKTOP-R7RU80H\SQLEXPRESS';
  $connectionInfo=array('Database'=>'WebPortal', 'UID'=>'WebAdmin', 'PWD'=>'P455w0rd');

  $conn = sqlsrv_connect($serverName, $connectionInfo);

    if ($conn ) {
      /*
        echo "Connection established.";
      */
    }else{
      echo "Connection could not be established.";
      die( print_r( sqlsrv_errors(), true));
    }

    echo $Today = date("Y-m-d");
    echo "</br> </br>";

  $sql = "SELECT * FROM Calendar_Data WHERE Date = '$Today'";

  $result = sqlsrv_query($conn, $sql);

  while($rowvar = sqlsrv_fetch_array($result)) 
    {
      $Financial_Year = ($rowvar['Financial_Year']);
      $Period = ($rowvar['Period']);
      $Week = ($rowvar['Week']);
      $FY_Start_Date = ($row['FY_Start_Date']);
      $FY_End_Date = ($row['FY_End_Date']);
      $Week_Start_Date = ($row['Week_Start_Date']);
      $Week_End_Date = ($row['Week_End_Date']);
      $Period_Start_Date = ($row['Period_Start_Date']);
      $Period_End_Date = ($row['Period_End_Date']);
    }

echo $rowvar;

  echo "Today's Date is ";
  echo $Today;
  echo "</br> </br>This Week's Start Date is ";
  echo $Week_Start_Date;
  echo "</br> </br>This Period's Start Date is ";
  echo $Period_End_Date;

?>

Dani AI

Generated

Most likely causes and a short checklist to fix them.

  • Typo/variable bug: the fetch uses one variable (e.g. $rowvar) but some assignments later use the wrong name (e.g. $row). That will leave the date variables unset. Also avoid echo $rowvar; — echoing an array prints nothing useful; use print_r() or var_dump() to inspect the fetched row and types.

  • SQL Server datetime handling: the sqlsrv driver returns SQL Server date/time columns as PHP DateTime objects. Echoing those directly will not print a readable date. Call ->format() on the DateTime instance before echoing.

  • Exact-match on datetime: comparing a datetime column to a Y-m-d string will fail when the DB value includes a time component. Either compare the date portion on the server side or use a BETWEEN that covers the whole day, or pass a proper date parameter.

Short, practical examples (do not reuse code from the OP):

$sql = "SELECT * FROM Calendar_Data WHERE CONVERT(date, [Date]) = ?";
$params = array($today);
$stmt = sqlsrv_query($conn, $sql, $params);
$row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC);

When outputting a returned datetime column:

if ($row['Week_Start_Date'] instanceof DateTime) {
  echo $row['Week_Start_Date']->format('Y-m-d');
} else {
  echo $row['Week_Start_Date'];
}

Quick debug checklist

  • Confirm $stmt is not false; call sqlsrv_errors() if it is.
  • Use SQLSRV_FETCH_ASSOC to avoid numeric keys.
  • print_r($row) to see exact column names and types.
  • Fix any $row vs $rowvar mismatches and ensure you echo the variable you actually set (also check you are not echoing Period end when you meant Period start).
  • If you expect a single row, use if instead of while to simplify logic.

This addresses the issues raised by and the data-question from ; following the checks above will usually reveal whether the problem is a typo, a type (DateTime) issue, or a query mismatch.

Recommended Answers

All 2 Replies

What does the data in your database look like? Line 20 of your code is retrieving all the data within the row(s) of the Calendar_Data table for today's date.

It looks like you marked this question as solved. Were you able to figure it out?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.