DMC
DMC

Reputation: 1194

Using PHP to connect to SQL Server 2005

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);
?>

Upvotes: 0

Views: 2042

Answers (1)

xdazz
xdazz

Reputation: 160953

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);

Upvotes: 2

Related Questions