4

I have recently switched a PHP app from mssql to sqlsrv and would like to continue using a couple custom functions to handle all my SQL requests. I get an error

Warning: sqlsrv_fetch_array(): 2 is not a valid ss_sqlsrv_stmt resource in...

when using the following function to handle all sqlsrv_query() calls:

<?php
function tko_query($sql)
{
    //Check for db connection
    $serverName = "server\sqlexpress";
    $connectionInfo = array( "Database"=>"db", "UID"=>"uid", "PWD"=>"pwd");
    $conn = sqlsrv_connect( $serverName, $connectionInfo );
    if( $conn === false ) {
        die( print_r( sqlsrv_errors(), true));
    }

    return sqlsrv_query($conn,$sql, array(), array('Scrollable' => 'buffered'));
}


$sql = "SELECT * FROM jobs";
$stmt = tko_query($sql);

while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) 
{
   echo $row['name']."<br />";
}

sqlsrv_free_stmt( $stmt);
?>
mu is too short
  • 426,620
  • 70
  • 833
  • 800
TravDog95
  • 41
  • 1
  • 1
  • 3
  • Seems like `'Scrollable' => 'buffered'` is not supported http://msdn.microsoft.com/de-de/library/ee376927(v=sql.90).aspx – sofl Jun 17 '13 at 13:14
  • Did you find out what the cause of the error was? – mzedeler Jun 17 '13 at 14:51
  • 1
    It seems that I may have found the answer here: http://stackoverflow.com/questions/16562704/php-sqlsrv-passing-a-resource-from-a-function – mzedeler Jun 17 '13 at 14:53

1 Answers1

0

You should declare $conn variable as global or try to fetch data inside tko_query function body. When tko_query ends the connection is closed and you cannot fetch data..

Sankumarsingh
  • 9,889
  • 11
  • 50
  • 74