I have a problem querying my MSSQL database and PHP7 I use the new Microsoft driver. I got the following error: sqlsrv_query() expects parameter 1 to be resource, null given in /var/www/html/sqlFunctions.php on line 33
If I put the 2 functions together in one function it works According to the microsft examples it should work https://docs.microsoft.com/nl-nl/sql/connect/php/step-3-proof-of-concept-connecting-to-sql-using-php
function OpenConnection()
{
try
{
$serverName = "devsql1";
$connectionOptions = array(
"Database"=>"test",
"Uid"=>"test",
"PWD"=>"test"
);
$conn = sqlsrv_connect($serverName, $connectionOptions);
if($conn == false)
die(FormatErrors(sqlsrv_errors()));
}
catch(Exception $e)
{
echo("Error!");
}
}
function ReadData()
{
try
{
$conn = OpenConnection();
$tsql = "SELECT [Corporatienaam] FROM tbl_Corporatie";
$getProducts = sqlsrv_query($conn, $tsql);
if ($getProducts == FALSE)
die(FormatErrors(sqlsrv_errors()));
$productCount = 0;
while($row = sqlsrv_fetch_array($getProducts, SQLSRV_FETCH_ASSOC))
{
echo($row['Corporatienaam']);
echo("<br/>");
$productCount++;
}
sqlsrv_free_stmt($getProducts);
sqlsrv_close($conn);
}
catch(Exception $e)
{
echo("Error!");
}
}