CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    Join Date
    Aug 2007
    Posts
    47

    Exclamation Catchable fatal error: Object of class DateTime could not be converted

    Hi...

    i'm testing php + mssql 2005 compatibility, using some DLLs microsoft relased which are php_sqlsrv.dll and php_sqlsrv_ts.dll

    i couldn't establish a connection with the first one, so i tried with the second one and it worked, i could run some queries perfectly until i got this error:

    Catchable fatal error: Object of class DateTime could not be converted to string in C:\Archivos de programa\Apache Group\Apache2\htdocs\asea\conectamssql.php on line 16

    i found out that when it reads a datetime or smalldatetime field it shows me that error, why? how could i make it work?
    thanks in advance...

    Here is the code:
    PHP Code:
    <?php
    $serverName 
    = ("myServer");
    $uid = ("userDB");
    $pwd = ("myPWD");
    $connectionInfo = array("UID" => $uid"PWD" => $pwd,"Database"=>"someDB");
    $conn sqlsrv_connect$serverName$connectionInfo);

    if( 
    $conn )
    {
         
    $result sqlsrv_query($conn,"select * from sometable");
         if (
    $result)
         {
            while(
    $row sqlsrv_fetch_array($result))
            {
               for (
    $i=0$i sqlsrv_num_fields($result); $i++)
    /*THIS IS LINE 16*/              echo $row[$i];
            }
         }
    }
    else
    {
         echo 
    "Connection could not be established.\n";
         die( 
    print_rsqlsrv_errors(), true));
    }
    sqlsrv_close$conn);
    ?>
    Last edited by PeejAvery; February 23rd, 2009 at 11:52 AM. Reason: Adde PHP tags.

  2. #2
    Join Date
    May 2002
    Posts
    10,943

    Re: Catchable fatal error: Object of class DateTime could not be converted

    Please remember to use code or php tags when posting code. Thanks!

    Is your column actually named DateTime? Remember that it is a reserved word and cannot be used when outputting to an object.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  3. #3
    Join Date
    Aug 2007
    Posts
    47

    Re: Catchable fatal error: Object of class DateTime could not be converted

    no, the field is named FechaPublicacion

  4. #4
    Join Date
    May 2002
    Posts
    10,943

    Re: Catchable fatal error: Object of class DateTime could not be converted

    Have you tried using the following instead?

    Code:
    while($row = sqlsrv_fetch_array($result, SQLSRV_FETCH_NUMERIC)) {
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  5. #5
    Join Date
    Aug 2007
    Posts
    47

    Re: Catchable fatal error: Object of class DateTime could not be converted

    yes, i have... it does the same...

    i've been reading that this php+sql 2005 driver is not the same as mssql.dll, it doesn't have num_rows(), data_seek(), etc...
    why???

    any suggestions?

  6. #6
    Join Date
    May 2002
    Posts
    10,943

    Re: Catchable fatal error: Object of class DateTime could not be converted

    Well, the differences would come from who programmed them. However, there is nothing in your code that should be returning that error.

    Try a single query that only the field that is DateTime. Echo that result to see if it is a data type conflict. PHP should automatically convert between data types.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  7. #7
    Join Date
    Aug 2007
    Posts
    47

    Re: Catchable fatal error: Object of class DateTime could not be converted

    PHP Code:
    $result sqlsrv_query($conn,"select fechapublicacion from sometable"); 
    i just queried the datetime field but it still does the same.. using SQLSRV_FETCH_NUMERIC and without...

  8. #8
    Join Date
    Aug 2007
    Posts
    47

    Question Re: Catchable fatal error: Object of class DateTime could not be converted

    i found a post through google search, it said i had to download ntwdblib.dll, copy it to system32 and use php_mssql.dll instead of sqlsrv.dll / sqlsrv_ts.dll in php.ini file

    and it's working.. without any problems with datetime fields..
    has any of you developed a web site using php & sql server 2005 with php_mssql.dll?
    can i "trust" it?

    i can't see why someone would use such driver for sql server 2005 if the "old" one does the job!
    does anyone know a reason for this?

    thanks in advance

  9. #9
    Join Date
    May 2002
    Posts
    10,943

    Re: Catchable fatal error: Object of class DateTime could not be converted

    What data type is that column? Is it DateTime?

    This article looks like it might be some help.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  10. #10
    Join Date
    Aug 2007
    Posts
    47

    Re: Catchable fatal error: Object of class DateTime could not be converted

    yes, i do a select * from table
    and there it comes such datatable field that messes up things with sqlsrv.dll and php...

    i haven't found a solution yet, other than starting to use php_mssql.dll instead...

    excellent article you sent me, thanks!

  11. #11
    Join Date
    Apr 2009
    Posts
    1

    Re: Catchable fatal error: Object of class DateTime could not be converted

    Hi, take a look at the sqlsrv_get_field function in the documentation.

    PHP Code:
    while ( $row sqlsrv_fetch$stmt))
    {
           echo 
    "Date: ".sqlsrv_get_field$stmt0
                           
    SQLSRV_PHPTYPE_STRINGSQLSRV_ENC_CHAR))."\n";


    Hope this helps!

  12. #12
    Join Date
    Aug 2007
    Posts
    47

    Thumbs up Re: Catchable fatal error: Object of class DateTime could not be converted

    thanks kbendyk, it worked!

    i'm using mssql though, sqlsrv doesn't have many useful methods like mssql does...
    nice to know how to make it work anyway...

    take care

  13. #13
    Join Date
    Jun 2009
    Posts
    1

    Re: Catchable fatal error: Object of class DateTime could not be converted

    another method is to use de php date_format() function to get the datetime as a string
    the datetime format returned by the PHP Driver is in fact a PHP DATETIME

    mydatestring = date_format(myfield, "d/m/Y");


    Hope this can help

  14. #14
    Join Date
    Aug 2007
    Posts
    47

    Re: Catchable fatal error: Object of class DateTime could not be converted

    thanks for your reply
    i'll give it a shot

  15. #15
    Join Date
    May 2010
    Posts
    1

    Re: Catchable fatal error: Object of class DateTime could not be converted

    I know this is old topic but someone may need this.

    I had the same problem and all i did was adding "ReturnDatesAsStrings" => false to connectionInfo. All fine now.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured