Log in

View Full Version : Resolved get value



bluewalrus
06-18-2009, 11:08 PM
Don't know if this should be in here or in the sql forum but this is where I've put it... I'm trying to get values from a mircosoft sql server, I can successfully retrieve 6 of the 9 using sqlsrv_get_field. However the last three just come up blank.

I've tried using this but it still doesn't work. http://msdn.microsoft.com/en-us/library/cc296207(SQL.90).aspx

The two items that won't show up are of type text and datetime which I think is the problem I attempt to use the stream solution at the bottom of that link but it Did not produce any code with it.

This is my code thus far. Thanks for any help you can offer.


<?php
/*Connect to the local server using Windows Authentication and
specify the AdventureWorks database as the database in use. */
$serverName = "SERVER";
$connectionInfo = array("Database" => "name");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn === false )
{
echo "Could not connect.\n";
die( print_r( sqlsrv_errors(), true));
}

/* Set up and execute the query. Note that both ReviewerName and
Comments are of the SQL Server nvarchar type. */

$tsql = "Select Top 5 dte, id, name, video, song, d, thumb From table where dte <= GetDate() order by dte Desc";
$stmt = sqlsrv_query( $conn, $tsql);
if( $stmt === false )
{
echo "Error in statement preparation/execution.\n";
die( print_r( sqlsrv_errors(), true));
}
/* Make the first row of the result set available for reading. */
if( sqlsrv_fetch( $stmt ) === false )
{
echo "Error in retrieving row.\n";
die( print_r( sqlsrv_errors(), true));
}

/* Note: Fields must be accessed in order.
Get the first field of the row. Note that no return type is
specified. Data will be returned as a string, the default for
a field of type nvarchar.*/
$dte1 = sqlsrv_get_field( $stmt, 0); /*DOESN'T WORK */
$tle1 = sqlsrv_get_field( $stmt, 1);
$aff1 = sqlsrv_get_field( $stmt, 2);
$aut1 = sqlsrv_get_field( $stmt, 3); /*DOESN'T WORK */
$strc1 = sqlsrv_get_field( $stmt, 4); /*DOESN'T WORK */
$vID1 = sqlsrv_get_field( $stmt, 5);
$id1 = sqlsrv_get_field( $stmt, 6);
echo "id: $tle1 <br />";
echo "tmb: $id1 <br />";
echo "title: $aff1 <br />";
echo "aut: $aut1 <br />"; /*displays aut:*/
echo "aff: $strc1 <br />"; /*displays aff: */
echo "vid: $vID1 <br />";
echo "date: $dte1 <br />"; /*displays nothing.... */

/* Free the statement and connection resources. */
sqlsrv_free_stmt( $stmt);
sqlsrv_close( $conn);
?>

Jesdisciple
06-19-2009, 12:51 AM
I attempt to use the stream solution at the bottom of that link but it Did not produce any code with it.How do you mean that it didn't produce any code?

Also, you said the "date:" line didn't print anything at all... That sounds like a PHP problem (possibly a result of the underlying SQL problem); even a NULL value shouldn't do that. See if this shows anything when placed at the script's start.
error_reporting(E_ALL | E_STRICT);
ini_set('display_errors', 1);

bluewalrus
06-19-2009, 01:04 AM
OO that's a helpful bit of code to getting errors. It gave me this. I don't know how to fix it though? Any ideas? Thanks.



Catchable fatal error: Object of class DateTime could not be converted to string in C:\pub\wwwroot\publish\Admin\test2.php on line 53

Also this is line 53

echo "date: $dte1 <br />"; /*displays nothing.... */

forum_amnesiac
06-19-2009, 07:20 AM
I came across this posting on another forum, if you go through it you'll see it mentions a couple of solutions to this error.

http://www.codeguru.com/forum/showthread.php?p=1851896

This part of one of the posts might be appropriate


while ( $row = sqlsrv_fetch( $stmt))
{
echo "Date: ".sqlsrv_get_field( $stmt, 0,
SQLSRV_PHPTYPE_STRING( SQLSRV_ENC_CHAR))."\n";

}

Jesdisciple
06-19-2009, 10:47 AM
:) Glad to pass on something I also got on this forum.

Note that I'm still curious how SQLSRV_PHPTYPE_STREAM( SQLSRV_ENC_CHAR) doesn't do what you expected.

bluewalrus
06-21-2009, 03:04 AM
I changed around the fetch and then it worked. Not exactly sure why but thanks.