Fragensteller
Problems with PHP accessing SQL database via query and fetch

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
- Typ geändert Raul TalmaciuMicrosoft contingent staff Mittwoch, 18. Januar 2012 14:02 Warten auf Feedback
- Typ geändert Raul TalmaciuMicrosoft contingent staff Donnerstag, 19. Januar 2012 07:14 Warten auf Feedback
Alle Antworten
-
Do you mind sharing the SQL query that you are executing? I can think of one situation where a cursor gets downgraded to a forward only cursor (depending on the query)...this forum thread has more details: http://social.msdn.microsoft.com/Forums/en-US/sqldriverforphp/thread/4a65fbe0-0c68-496b-bf72-2d3eec9f31a3/
Any information that will hep reproduce what you are seeing will help us get to the bottom of it quickly.
Thanks.
-Brian
This posting is provided "AS IS" with no warranties, and confers no rights. http://blogs.msdn.com/brian_swan -
Hi,
please note that this is a German forum. If English is your preffered language, then please post on the English forums:
http://social.technet.microsoft.com/Forums/en/categories/
Regards,
RaulRaul Talmaciu, MICROSOFT
Bitte haben Sie Verständnis dafür, dass im Rahmen dieses Forums, welches auf dem Community-Prinzip „IT-Pros helfen IT-Pros“ beruht, kein technischer Support geleistet werden kann oder sonst welche garantierten Maßnahmen seitens Microsoft zugesichert werden können.