0

I have the problem to retrieve information from SQL server database. But no problem to insert data.

this is the simple code:

$serverName = "xx.xx.xx.xx\SQLEXPRESS,1433";
$connectionOptions = array(
    "Database" => "Test",
    "Uid" => "User",
    "PWD" => "Password"
);

$connection = sqlsrv_connect($serverName, $connectionOptions);

if (!connection) {
    die("Database connection failed: " . mssql_get_last_message() ); 
} else {
    echo("Connected Successfully </br>" );
}

$sql = " INSERT INTO Imaging (ap) VALUES (1)"; 
$result = sqlsrv_query( $connection, $sql);

This code working perfectly.

But if I trying to retrieve data

$sql = "select ap from Imaging ";
$result = sqlsrv_query( $connection, $sql);

var_dump($result);

I have this result. Connected Successfully resource(2) of type (SQL Server Statement)

What is wrong?

Thank you

3
  • var_dump($result); what does this print? Commented Dec 7, 2017 at 16:51
  • Connected Successfully resource(2) of type (SQL Server Statement) Commented Dec 7, 2017 at 16:52
  • Thank you WillParkky93. It's working! Commented Dec 7, 2017 at 16:58

1 Answer 1

1

That's because $result is a resource;
From the man page of sqlsrv_fetch_array your code must be like so:

while( $row = sqlsrv_fetch_array( $result, SQLSRV_FETCH_ASSOC) ) {    
    echo $row['ap'];    
}
Sign up to request clarification or add additional context in comments.

Comments

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.