Question

So before I used the sqlsrv function I was using the MySQL function.

The code worked perfect for me, but now that i have replaced the MySQL function by sqlsrv it gives me this error.

sqlsrv_fetch_array() expects parameter 2 to be long

The code is used to count is this:

$conn = sqlsrv_connect( $serverName, $connectionInfo);
$tsql = "SELECT * FROM login WHERE username = '".($_SESSION['username'])."'";
$res = sqlsrv_query($conn, $tsql);
while($row = sqlsrv_fetch_array($res, SQLSRV_FETCH_ASSOC)) {

$aantalATV = $row['ATV'];
$aanvragenATV = "SELECT COUNT(soort) AS totaal FROM goedgekeurd WHERE username = '".($_SESSION['username'])."' AND soort='ATV' ";
$rowATV = sqlsrv_fetch_array($conn, $aanvragenATV);
$aantalATV= $aantalATV - $rowATV[0];
echo "aantal ATV dagen resterende:" .$aantalATV."<br />";
}

where is the problem, is it the query that needs to be changed?

Any help is appreciated!

New code snippet based on the answer of: Rikesh

$aantalATV = $row['ATV'];
$aanvragenATV = "SELECT COUNT(soort) AS totaal FROM goedgekeurd WHERE  username = '".($_SESSION['username'])."' AND soort='ATV' ";
$res = sqlsrv_query($conn, $aanvragenATV);
$rowATV = sqlsrv_fetch_array($res, SQLSRV_FETCH_NUMERIC);
$aantalATV= $aantalATV - $rowATV[0];
echo "aantal ATV dagen resterende:" .$aantalATV."<br />";
Was it helpful?

Solution

You using sqlsrv_fetch_array incorrectly second time.

sqlsrv_fetch_array($conn, $aanvragenATV);

sqlsrv_fetch_array take first parameter as sql statement & second fetch type. So change it to,

sqlsrv_fetch_array($aanvragenATV,SQLSRV_FETCH_NUMERIC);

Note: It seems from your code you have used $rowATV[0] you need to fetch it by type NUMERIC or else change it as per your need.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top