Reputation: 34055
I can't seem to figure out why sqlsrv_fetch_assoc
only returns 1 row when there are 2 rows in the result set according to sqlsrv_num_rows
. I've tried to hard code the parameter, but I still get the same result.
SSMS Result Set
id description ------ ------------- 2 Administrator 3 User
PHP
$col = 'abcd';
$stmt = "SELECT id, [description] FROM dbo.tbl WHERE col = ?";
$params = array( $col );
$options = array( "Scrollable" => SQLSRV_CURSOR_KEYSET );
$query = sqlsrv_query( $conn, $stmt, $params, $options );
if( $query === false ) {
print( print_r( sqlsrv_errors() ) );
}
while( $row = sqlsrv_fetch_array( $query, SQLSRV_FETCH_ASSOC )) {
...
}
When I try to view the result set
$row = sqlsrv_fetch_array( $query, SQLSRV_FETCH_ASSOC );
print_r($row);
I receive
Array ( [id] => 2 [description] => Administrator )
sqlsrv_num_rows
echo sqlsrv_num_rows( $query );
//Returns 2
Upvotes: 6
Views: 27688
Reputation: 1
I was getting that issue. I realized there was probably a special character that was causing the "while" statement to trip on the query result.
In my case, I had a varchar field but I suppose it could happen with any char field.
select replace(replace(replace(replace(replace(replace(Content,char(146),' '),char(147),' '),char(148),' '),char(149),' '),char(150),' '),char(151),' ')
from MyTable
Then I got all my results. Cannot claim if all the characters listed here caught everything (Note LF/0x0a
works fine) and show up properly.
This was with the linux version of PHP.
Upvotes: -2
Reputation: 1
$sql = "SELECT id, description FROM dbo.tbl";
$stmt = sqlsrv_query( $conn, $sql );
if( $stmt === false) {
die( print_r( sqlsrv_errors(), true) );
}
$data = array();
while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {
$data[] = array(
"description" => $row['description']
);
}
return $data;
Upvotes: 0
Reputation: 412
This code serve me finally Thanks to "Ashish". I don't understand why its not upvoted.
$Result = array();
$sql = "EXEC CRM_TestSP;";
$stmt = sqlsrv_query( $conn, $sql );
if( $stmt === false) {
die( print_r( sqlsrv_errors(), true) );
}
$i = 0;
while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {
$Result[$i]["Description"] = $row['Description'];
$i++;
}
sqlsrv_free_stmt($stmt);
return $Result;
It returns an array like this:
array(2) { [0]=> array(1) { ["Description"]=> string(4) "Text 1" } [1]=> array(1) { ["Description"]=> string(4) "Text2" } } 1
That's exactly what I'm looking for.
Upvotes: 1
Reputation: 39
A simple way is to use sqlsrv_fetch_array and use multi dimensional array to store each row data. But on doing so make sure to use $i++ instead of $i += 1 If we use $i +=1 then only single row is saved.
$i = 0;
while( $row = sqlsrv_fetch_array( $query, SQLSRV_FETCH_ASSOC )) {
$result[$i][0] = $row['id'];
$result[$i][1] = $row['description'];
$i++; //works
// $i += 1; //does not work with this !
}
var_dump($result);
Upvotes: 1
Reputation: 107696
When I try to view the result set
$row = sqlsrv_fetch_array( $query, SQLSRV_FETCH_ASSOC );
print_r($row);`
Since you listed this separately, I hope you're not doing this (showing all code):
$col = 'abcd';
$stmt = "SELECT id, [description] FROM dbo.tbl WHERE col = ?";
$params = array( $col );
$options = array( "Scrollable" => SQLSRV_CURSOR_KEYSET );
$query = sqlsrv_query( $conn, $stmt, $params, $options );
if( $query === false ) {
print( print_r( sqlsrv_errors() ) );
}
while( $row = sqlsrv_fetch_array( $query, SQLSRV_FETCH_ASSOC )) {
...
$row = sqlsrv_fetch_array( $query, SQLSRV_FETCH_ASSOC );
print_r($row);
}
Because then it's obvious you're consuming sqlsrv_fetch_array twice per loop iteration, once for the condition check and once inside the loop.
Remove all fluff from the while loop and have just this - and absolutely nothing else, not even the ... or comments.
while( $row = sqlsrv_fetch_array( $query, SQLSRV_FETCH_ASSOC )) {
print_r($row);
}
Upvotes: 7
Reputation: 34107
sqlsrv_fetch_array
: Returns the next available row of data as an associative array, a numeric array, or both (the default).
Emphasis by me.
Return Values: Returns an array on success, NULL if there are no more rows to return, and FALSE if an error occurs.
You'll need to do a while loop to fetch all records, like this:
while ($row = sqlsrv_fetch_array( $query, SQLSRV_FETCH_ASSOC )) {
//process $row
}
Upvotes: 3
Reputation: 13586
I'm under the impression that fetch_array
only retrieves a row at a time (a row being the NEXT row)... Try something like...
while($row = sqlsrv_fetch_array( $query, SQLSRV_FETCH_ASSOC );){
//do something awesome
}
Upvotes: 0