在一台机器上连接 XAMPP 到 Microsoft sql Server Management Studio 2012

Connect XAMPP to microsoft sql server management studio 2012 in one machine

我无法使用 XAMPP 将 php 连接到 Microsoft SQL Server Management Studio 2012。我正在使用单独的数据库(不是 phpmyadmin) 我已经在 XAMPP.

中关闭了 mysql

我的 xampp 和 数据库没有密码,因为我曾尝试访问 mysql。 当我 运行 php 代码时出现以下错误。 警告:mysqli_connect(): (HY000/2002): 无法建立连接,因为目标机器主动拒绝。

我的代码:

$server = 'localhost';
$username= 'sa';
$password = '';
$db= 'CASA';
$conn= mysqli_connect($server, $username, $password ,$db);

提前致谢。

您首先需要安装或(if already installed) the sqlserver php driver and then you could try something like below which i have just taken from Microsoft documentation

<?php  
   $serverName = "(local)";   
   $database = "AdventureWorks";  

   // Get UID and PWD from application-specific files.   
   $uid = file_get_contents("C:\AppData\uid.txt");  
   $pwd = file_get_contents("C:\AppData\pwd.txt");  

   try {  
      $conn = new PDO( "sqlsrv:server=$serverName;Database = $database", $uid, $pwd);   
      $conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );   
   }  

   catch( PDOException $e ) {  
      die( "Error connecting to SQL Server" );   
   }  

   echo "Connected to SQL Server\n";  

   $query = 'select * from Person.ContactType';   
   $stmt = $conn->query( $query );   
   while ( $row = $stmt->fetch( PDO::FETCH_ASSOC ) ){   
      print_r( $row );   
   }  

   // Free statement and connection resources.   
   $stmt = null;   
   $conn = null;   
?> 

希望对你有帮助