简体   繁体   English

在PHP中无法连接到MSSQL

[英]Connection failed to MSSQL in php

I'm using SQL Server 2014 and I have this code to connect to my sql server : 我正在使用SQL Server 2014,并且我有以下代码连接到我的sql服务器:

<?php 

$serverName = "DESKTOP-P87SVPI\MSSQLSERVER";

$connectionInfo = array("Database"=>"map2");

$conn = sqlsrv_connect($serverName,$connectionInfo);

if ($conn){
echo "finally";
}
else {
print_r( sqlsrv_errors());

}
?>

I got this error : 我收到此错误:

Array ( [0] => Array ( [0] => 08001 [SQLSTATE] => 08001 1 => 87 [code] => 87 2 => [Microsoft][ODBC Driver 11 for SQL Server]SQL Server Network Interfaces: Connection string is not valid [87]. [message] => [Microsoft][ODBC Driver 11 for SQL Server]SQL Server Network Interfaces: Connection string is not valid [87]. ) 1 => Array ( [0] => HYT00 [SQLSTATE] => HYT00 1 => 0 [code] => 0 2 => [Microsoft][ODBC Driver 11 for SQL Server]Login timeout expired [message] => [Microsoft][ODBC Driver 11 for SQL Server]Login timeout expired ) 2 => Array ( [0] => 08001 [SQLSTATE] => 08001 1 => 87 [code] => 87 2 => [Microsoft][ODBC Driver 11 for SQL Server]A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online. [message] => [Microsoft][ODBC Driver 11 for SQL Server]A network-re Array([0] => Array([0] => 08001 [SQLSTATE] => 08001 1 => 87 [code] => 87 2 => [Microsoft] [用于SQL Server的ODBC驱动程序11] SQL Server网络接口:连接字符串无效[87]。[消息] => [Microsoft] [用于SQL Server的ODBC驱动程序11] SQL Server网络接口:连接字符串无效[87]。) 1 =>数组([0] => HYT00 [SQLSTATE] => HYT00 1 => 0 [代码] => 0 2 => [Microsoft] [用于SQL Server的ODBC驱动程序11]登录超时已过期[消息] => [Microsoft] [用于SQL Server的ODBC驱动程序11]登录超时已过期) 2 =>数组([0] => 08001 [SQLSTATE] => 08001 1 => 87 [code] => 87 2 => [Microsoft] [SQL Server的ODBC驱动程序11]与网络相关或建立与SQL Server的连接时发生实例特定的错误;找不到服务器或无法访问服务器;检查实例名称是否正确;是否将SQL Server配置为允许远程连接;有关更多信息,请参见SQL Server联机丛书。 ] => [Microsoft] [用于SQL Server的ODBC驱动程序11] lated or instance-specific error has occurred while establishing a connection to SQL Server. 建立与SQL Server的连接时发生了特定错误或特定于实例的错误。 Server is not found or not accessible. 找不到服务器或无法访问服务器。 Check if instance name is correct and if SQL Server is configured to allow remote connections. 检查实例名称是否正确以及SQL Server是否配置为允许远程连接。 For more information see SQL Server Books Online. 有关更多信息,请参见SQL Server联机丛书。 ) ) ))

I checked this in MSSQL: 我在MSSQL中检查了这一点:

select @@servername + '\' + @@servicename

And it's the same : 而且是一样的:

DESKTOP-P87SVPI\MSSQLSERVER

And These are the drivers I have in phpInfo(): 这些是我在phpInfo()中拥有的驱动程序: 在此处输入图片说明

Also I checked the remote connection in MSSQL and it's allowed. 我也检查了MSSQL中的远程连接,并允许它。

How to fix this ? 如何解决呢?

Aer u sure that your server is not using password? 确定您的服务器未使用密码?

Try to hook up the conenction with PDO: 尝试与PDO挂钩:

    <?php
  try {
    $hostname = "localhost/MSSQLSERVER";
    $port = 10060;
    $dbname = "dbname";
    $username = "dbuser";
    $pw = "password";
    $dbh = new PDO ("dblib:host=$hostname:$port;dbname=$dbname","$username","$pw");
  } catch (PDOException $e) {
    echo "Failed to get DB handle: " . $e->getMessage() . "\n";
    exit;
  }
  $stmt = $dbh->prepare("select name from master..sysdatabases where name = db_name()");
  $stmt->execute();
  while ($row = $stmt->fetch()) {
    print_r($row);
  }
  unset($dbh); unset($stmt);
?>

Hope this helps. 希望这可以帮助。

That should do the Job, if not it has something to do with the setup from your SQL-Server! 那应该做的工作,如果不是,那与您的SQL Server设置有关!

I used this code to connect but also having problem so I added ConnectionPooling=0 in the connection then it worked ! 我使用此代码进行连接,但是也遇到了问题,因此我在连接中添加了ConnectionPooling=0 ,然后它起作用了!

 try {


$conn = new PDO("sqlsrv:server=serverName\serverInstance,1433;Database=db;ConnectionPooling=0", "user", "pass");

} catch (PDOException $e) {

    echo $e->getMessage();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM