0

Hi I am using a PHP script to connect to SQL Server 2005. After much tinkering around I can finally establish a connection. However when I try and query the database I am getting no response. I have tested my SQL Query on the database and it runs fine. Any help greatly appreciated. Thanks

<?php
/

$serverName = "*******";
$usr="******";
$pwd="****";
$db="*****";



//Connection to Database
$connectionInfo = array("UID" => $usr, "PWD" => $pwd, "Database" => $db);

$conn = sqlsrv_connect($serverName, $connectionInfo);
if( $conn )
{
     echo "Connection to database established.\n";

}
else
{
     echo "Connection could not be established.\n";
     die( print_r( sqlsrv_errors(), true));
}

//-----------------------------------------------
// Perform operations with connection.
//-----------------------------------------------

$sql = "SELECT ContactName FROM dbo.TBL_JOB WHERE EngineerID = 1 ";


/* Close the connection. */
sqlsrv_close( $conn);
?>
1
  • 3
    Where is the query code, I can only see a string $sql ? Commented Jan 26, 2012 at 10:18

1 Answer 1

2

Add the code below after $sql = "SELECT ContactName FROM dbo.TBL_JOB WHERE EngineerID = 1 ";:

$stmt = sqlsrv_query($conn, $sql );
if ($stmt === false) {
     die(print_r(sqlsrv_errors(), true));
}

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

sqlsrv_free_stmt($stmt);
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.