简体   繁体   中英

sqlsrv_fetch_array doesn't return correct rows

I'm trying to SELECT some products from a remote MS SQL Server with PHP.

sqlsrv_num_rows() returns correct number of records, but sqlsrv_fetch_array() in some cases returns 1 or 2 records and in most of cases returns nothing.

This is my code:

ini_set('max_execution_time', 1800);
ini_set('display_errors', 1);
header("Content-type: text/html; charset=utf-8");
error_reporting(E_ERROR | E_WARNING | E_PARSE);

$server = "servername";

$connectionInfo = array( "Database"=>"dbname", "UID"=>"username", "PWD"=>"password");
$conn = sqlsrv_connect( $server, $connectionInfo);

if( $conn ) {
     echo "Connection established.<br />";
}else{
     echo "Connection could not be established.<br />";
     die( print_r( sqlsrv_errors(), true));
}

$sql = "SELECT items.mtrl, items.code, items.code2 model, items.name name1 from mtrl as items LEFT OUTER JOIN mtrextra as extra on extra.mtrl=items.mtrl WHERE extra.utbl01=342 AND extra.bool01=1 AND items.sodtype=51 ORDER BY items.mtrl";

$params = array();
$options =  array( "Scrollable" => 'static');
$result = sqlsrv_query( $conn, $sql , $params, $options );

$rowCount = sqlsrv_num_rows($result);

while( $row = sqlsrv_fetch_array($result,SQLSRV_FETCH_ASSOC)) {
echo $row['code']." | ".$row['name1']."<br/>";
}
echo "<p>".$rowCount."</p>";
echo print_r(sqlsrv_errors(), true);

This is the page url: https://www.eleftherohori.gr/test/sqlsrv.php

Also I have a error message from this script echo print_r(sqlsrv_errors(), true);

Array ( [0] => Array ( [0] => 01004 [SQLSTATE] => 01004 1 => 0 [code] => 0 [2] => [Microsoft][ODBC Driver 13 for SQL Server]String data, right truncation [message] => [Microsoft][ODBC Driver 13 for SQL Server]String data, right truncation ) )

Can anyone help me with this? Thank you.

in sqlsrv_fetch_array() a fetch type of SQLSRV_FETCH_ASSOC should not be used when consuming a result set with multiple columns of the same name. SQLSRV_FETCH_BOTH (the default). Check this link https://www.php.net/manual/en/function.sqlsrv-fetch-array.php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM