sql server - PHP- Error with SQLSRV_FETCH_ASSOC & sqlsrv_fetch_array -


this code using download result sql server query in excel . connection & query works fine , have installed sqlsrv extensions properly. still getting error these

"use of undefined constant sqlsrv_fetch_assoc - assumed sqlsrv_fetch_assoc'"
"warning: sqlsrv_fetch_array() expects parameter 2 long"

i think problem sqlsrv_fetch_assoc. appreciated.

  function cleandata(&$str)   {     $str = preg_replace("/\t/", "\\t", $str);     $str = preg_replace("/\r?\n/", "\\n", $str);     if(strstr($str, '"')) $str = '"' . str_replace('"', '""', $str) . '"';   }    $servername = "********";  $connectioninfo = array( "database"=>"****", "uid"=>"***", "pwd"=>"*****"); $connect = sqlsrv_connect( $servername, $connectioninfo);    // filename download   $filename = "website_data_" . date('ymd') . ".xls";    header("content-disposition: attachment; filename=\"$filename\"");   header("content-type: application/vnd.ms-excel");    $flag = false;   $query="select * business_partners order 1 desc";    $result = sqlsrv_query($connect, $query) or die('a error occured: ' . sqlsrv_errors());   while(false !== ($row = sqlsrv_fetch_array($result,sqlsrv_fetch_assoc)))    {     if(!$flag) {       // display field/column names first row       echo implode("\t", array_keys($row)) . "\r\n";       $flag = true;     }**strong text**     array_walk($row, 'cleandata');     echo implode("\t", array_values($row)) . "\r\n";   }   exit; 

i have no explanation why constant wouldn't defined while extension loaded, should able circumvent problem defining yourself.

sqlsrv_fetch_assoc has value 2, should add somewhere, @ best in file include always:

if ( !defined( 'sqlsrv_fetch_assoc' ) )   define( 'sqlsrv_fetch_assoc', 2 ); 

the if ( !defined() ) clause makes sure code doesn't break later, when actual problem fixed , constant reappears.

alternatively, if use once in file , isn't worth defining constant (except readability) can use actual value parameter:

$row = sqlsrv_fetch_array( $result, 2 ); // 2 = sqlsrv_fetch_assoc 

Comments

Popular posts from this blog

sql - VB.NET Operand type clash: date is incompatible with int error -

SVG stroke-linecap doesn't work for circles in Firefox? -

python - TypeError: Scalar value for argument 'color' is not numeric in openCV -