-
February 23rd, 2009, 11:25 AM
#1
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_r( sqlsrv_errors(), true)); } sqlsrv_close( $conn); ?>
Last edited by PeejAvery; February 23rd, 2009 at 11:52 AM.
Reason: Adde PHP tags.
-
February 23rd, 2009, 11:56 AM
#2
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.
-
February 23rd, 2009, 11:59 AM
#3
Re: Catchable fatal error: Object of class DateTime could not be converted
no, the field is named FechaPublicacion
-
February 23rd, 2009, 01:54 PM
#4
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.
-
February 23rd, 2009, 02:05 PM
#5
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?
-
February 23rd, 2009, 02:11 PM
#6
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.
-
February 23rd, 2009, 03:15 PM
#7
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...
-
February 23rd, 2009, 04:53 PM
#8
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
-
February 23rd, 2009, 05:01 PM
#9
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.
-
February 26th, 2009, 10:56 AM
#10
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!
-
April 9th, 2009, 08:56 AM
#11
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( $stmt, 0,
SQLSRV_PHPTYPE_STRING( SQLSRV_ENC_CHAR))."\n";
}
Hope this helps!
-
April 9th, 2009, 05:18 PM
#12
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
-
June 16th, 2009, 10:15 AM
#13
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
-
June 16th, 2009, 11:01 AM
#14
Re: Catchable fatal error: Object of class DateTime could not be converted
thanks for your reply
i'll give it a shot
-
May 18th, 2010, 04:07 PM
#15
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|