none
Problems with PHP accessing SQL database via query and fetch RRS feed

  • Allgemeine Diskussion

  • Hi,

    sorry I am a quite inexperienced programmer and my following question may be extremely stupid. Howeve, I have worked on this now for several hours without success and don't find my error.

    The task is very simple, I just want to access data rows from my SQL database and get them as arrays into my PHP script. Here is an extract from the script:

     $sql="SELECT TOP 1 * FROM TABLE";
     $queryoptions=array("Scrollable","SQLSRV_CURSOR_STATIC");
     $result=sqlsrv_query($connect,$sql,$queryoptions);

    echo $result;

     

    The last statement returns " Resource id #4" so the query seems to work.

    resultfielddata=sqlsrv_field_metadata($result);

    print_r($resultfielddata);

    The last statement returns an array with correct metadata from the table. So the column information is correctly retrieved.

     $hasrows=sqlsrv_has_rows($result);

       echo $hasrows;


    The last statement returns "1" so I assume that there are data-rows in the table (there should be one row of data).

    So far so good.

    In the next step I want to get the data from the first row of the resource as an array:

    $case=sqlsrv_fetch_array($result,SQLSRV_FETCH_ASSOC);

    However this returns "FALSE" and print_r(sqlsrv_errors()); returns:

    Array ( [0] => Array ( [0] => 01004 [SQLSTATE] => 01004 [1] => 0 [code] => 0 [2] => [Microsoft][SQL Server Native Client 10.0]String data, right truncation [message] => [Microsoft][SQL Server Native Client 10.0]String data, right truncation ) )

     

    When I try to explore the $result array using

    $row_count = sqlsrv_num_rows($result);

    $row_count is false and the error array is:

    Array ( [0] => Array ( [0] => IMSSP [SQLSTATE] => IMSSP [1] => -50 [code] => -50 [2] => This function only works with statements that have static or keyset scrollable cursors. [message] => This function only works with statements that have static or keyset scrollable cursors. ) )

    However, I defined the cursor at the beginning!!

     

    So I am really stuck here and would be very glad if someone could help me out of this stupid problem. Why is the data not read into the array?

    Thanks a lot in advance!!

     

    Best regards

    filhelsinki

    Mittwoch, 11. Januar 2012 21:13

Alle Antworten