dsrbb20862 2012-01-26 10:16
浏览 10
已采纳

使用PHP连接到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.
";

}
else
{
     echo "Connection could not be established.
";
     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条回答 默认 最新

  • doumenshi1475 2012-01-26 10:20
    关注

    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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探