0

I want to execute a simple sqlsrv_query query in php. To do so, I installed the MS Drivers and was able to establish a connection to my DB (SQL Server Management Studio 18.9.2). What I don´t understand is the message Resource id #3, which I believe is neither an failure or error. I expect a raw echo / print_r of the table content. What do I miss?

$sql = "SELECT * FROM nodes";
$stmt = sqlsrv_query($conn, $sql);

if ($stmt === false) {
    die (print_r(sqlsrv_errors(), true));
} else {
    while ($row = sqlsrv_fetch($stmt))
    print_r($row);
    print_r($stmt);
    echo "<br />Statement executed";
}

The table looks like:

enter image description here

Error I get:

enter image description here

1 Answer 1

1

Try to use sqlsrv_fetch_array statement instead of the sqlsrv_fetch.

while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC))
{
    // print_r($row);
    print($row["nodes"] . "<br>");
}
Sign up to request clarification or add additional context in comments.

3 Comments

I think a better method can be using PDO instead of SQLSRV functions.
thanks for your hint which I tested. The ressource #id 3 message is gone but I also do not receive any further result. Just pure blank, after the "Connection established" message.
I modified the answer, try it please. I think the name of your column is "nodes".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.