Results 1 to 6 of 6

Thread: get value

  1. #1
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default get value

    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/libr...7(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 Code:
    <?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_rsqlsrv_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_rsqlsrv_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_rsqlsrv_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$stmt0); /*DOESN'T WORK */
    $tle1 sqlsrv_get_field$stmt1);
    $aff1 sqlsrv_get_field$stmt2);
    $aut1 sqlsrv_get_field$stmt3); /*DOESN'T WORK */
    $strc1 sqlsrv_get_field$stmt4); /*DOESN'T WORK */
    $vID1 sqlsrv_get_field$stmt5);
    $id1 sqlsrv_get_field$stmt6);
    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);
    ?>
    Last edited by bluewalrus; 06-21-2009 at 03:05 AM.

  2. #2
    Join Date
    Jul 2006
    Posts
    497
    Thanks
    8
    Thanked 70 Times in 70 Posts

    Default

    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.
    Code:
    error_reporting(E_ALL | E_STRICT);
    ini_set('display_errors', 1);
    -- Chris
    informal JavaScript student of Douglas Crockford
    I like wikis - a lot.

  3. The Following User Says Thank You to Jesdisciple For This Useful Post:

    bluewalrus (06-19-2009)

  4. #3
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    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.... */
    Last edited by bluewalrus; 06-19-2009 at 01:23 AM.

  5. #4
    Join Date
    Apr 2009
    Location
    Cognac, France
    Posts
    400
    Thanks
    2
    Thanked 57 Times in 57 Posts

    Default

    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

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

  6. The Following User Says Thank You to forum_amnesiac For This Useful Post:

    bluewalrus (06-21-2009)

  7. #5
    Join Date
    Jul 2006
    Posts
    497
    Thanks
    8
    Thanked 70 Times in 70 Posts

    Default

    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.
    -- Chris
    informal JavaScript student of Douglas Crockford
    I like wikis - a lot.

  8. The Following User Says Thank You to Jesdisciple For This Useful Post:

    bluewalrus (06-21-2009)

  9. #6
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    I changed around the fetch and then it worked. Not exactly sure why but thanks.

Bookmarks

Posting Permissions

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