SQL Server Developer Center > SQL Server Forums > SQL Server Driver for PHP > problem DateTime Select query no result
Ask a questionAsk a question
 

Answerproblem DateTime Select query no result

  • Tuesday, October 06, 2009 8:58 PMBB888 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Vista=32bit, W2008 R2 64bit, SQLEXPRESS 2008 SP1 64bit
    XAMPP 1.7.2 php 5.3
    PHP Driver 1.1
    There is a valid day in database, but it does not filter it. With SQL Management Studio SELECT is ok and returns a record, but with php there is no record
    Is there a language problem SETLOCALE or a config problem ?.
    The same code is working on a Vista machine (returns one record) , but on W2008 not (returns no record).

    perhaps SETLOCAL(..) ?
    Thank you very much.



    $tsql = "Select * from Preise Where (Day=?) ";
    $params = array($Day);

    $stmt = sqlsrv_query( $conn, $tsql, $params);
    if ( $stmt )
    {
    }
    else
    {
       echo "Error in statement execution.\n";
       die( print_r( sqlsrv_errors(), true));
    }
    while($Row = sqlsrv_fetch_array($stmt))

Answers

  • Wednesday, October 28, 2009 12:06 AMSerban Iliescu - MSFTAnswererUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    It will be interesting to know what type of data is in the database for “Day”. If it is a DateTime type, then the problem might be that the PHP driver defaults this type to a PHP DateTime object. If your code uses strings, then the supplied parameter might not be appropriate. The simplest workaround is to connect using option “ReturnDatesAsStrings” set to true.

     

    Additionally, please identify the driver call that fails (i.e. either sqlsrv_query or sqlsrv_fetch_array) and post the error information subsequently returned by sqlsrv_errors().

     

    Hope this helps,

    Serban


    Serban Iliescu - MSFT

All Replies

  • Thursday, October 08, 2009 8:57 AMCharles Wang - MSFTMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code

    How do you judge that the statements in the while loop not executed?  I performed a simple test with the following code on Windows 7, it worked fine. But need to make sure that the character's lower/upper case of the column name macthed the column name in your select string.  If I use $row['Name'] to get the data, it will return NULL.

    <?php
    $servername="Charles-Win7";
    $connectionInfo = array("UID"=>"test","PWD"=>"Password01!","Database"=>"tt1");
    $day=date('Y-m-d');
    $params=array(array($day, null, null, SQLSRV_SQLTYPE_DATETIME));
    $sql="SELECT ID,NAME,CreateDay FROM Preise WHERE CreateDay=?";
    $conn = sqlsrv_connect($servername,$connectionInfo);
    if($conn==false)
    {
      echo "could not connect.<br/>";
      die(print_r(sqlsrv_errors(),true));
    }
    else
      echo "connection successful<br/>";
    $stmt = sqlsrv_query($conn,$sql,$params);
    
    if($stmt==false)
    {
      echo "Error in query!<br/>";
      die(print_r(sqlsrv_errors(),true));
    }
    else
      echo "query execution successful.<br/>";
    while($row=sqlsrv_fetch_array($stmt,SQLSRV_FETCH_ASSOC))
    {  
      echo $row['NAME']."</br>";
    }
    
    sqlsrv_free_stmt($stmt);
    sqlsrv_close($conn);
    ?>
    
    
    

    Please remember to mark the replies as answers if they help and unmark them if they provide no help
  • Wednesday, October 28, 2009 12:06 AMSerban Iliescu - MSFTAnswererUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    It will be interesting to know what type of data is in the database for “Day”. If it is a DateTime type, then the problem might be that the PHP driver defaults this type to a PHP DateTime object. If your code uses strings, then the supplied parameter might not be appropriate. The simplest workaround is to connect using option “ReturnDatesAsStrings” set to true.

     

    Additionally, please identify the driver call that fails (i.e. either sqlsrv_query or sqlsrv_fetch_array) and post the error information subsequently returned by sqlsrv_errors().

     

    Hope this helps,

    Serban


    Serban Iliescu - MSFT