【问题标题】:ODBC connecting to SQL Server PHP Error on a linux serverODBC 连接到 Linux 服务器上的 SQL Server PHP 错误
【发布时间】:2021-05-14 11:23:03
【问题描述】:

我正在尝试连接到 sql 服务器,但收到未安装 odbc 的错误。但是在我的服务器上为 php 安装了 odbc 和 sqlsrv 扩展。我正在使用 PHP 7.4.11 和 ODBC 2.3.1。请帮助。

这是我用来连接的代码

$serverName = $serverIP; //serverName\instanceName
$connectionInfo = array( "Database"=>$db, "UID"=>$user, "PWD"=>$pwd, "CharacterSet" => "UTF-8");
$conn = sqlsrv_connect( $serverName, $connectionInfo) ;

if( $conn ) {
     echo "Connection established.<br />";
}else{
     echo "Connection could not be established.<br />";
     die( print_r( sqlsrv_errors(), true));
}

我得到的错误信息是:

Connection could not be established.
Array ( [0] => Array ( [0] => IMSSP [SQLSTATE] => IMSSP [1] => -49 [code] => -49 [2] => This extension requires the Microsoft ODBC Driver for SQL Server. Access the following URL to download the ODBC Driver for SQL Server for x64: https://go.microsoft.com/fwlink/?LinkId=163712 [message] => This extension requires the Microsoft ODBC Driver for SQL Server. Access the following URL to download the ODBC Driver for SQL Server for x64: https://go.microsoft.com/fwlink/?LinkId=163712 ) )

我可以通过我的本地系统使用与 windows 上的 xampp 相同的代码连接到数据库

【问题讨论】:

  • 你是想神奇地连接还是有一些代码来说明你的连接?
  • @Explisam,对不起,我忘了提。现在我已经编辑了我的问题,请检查
  • 发布给你的确切错误信息。
  • @Explisam 当然。
  • 错误消息明确指出,就 PHP 扩展而言,Microsoft 的“SQL Server ODBC 驱动程序”未安装在运行 PHP 应用程序的服务器上。是吗?

标签: php linux odbc sqlsrv


【解决方案1】:

我终于成功建立了数据库连接。 This 帮了很多忙。 首先,我必须在我的主目录中创建一个 odbc.ini 文件,因为我使用共享主机来托管多个网站,所以这个文件应该放在主域文件管理器中。 文件 odbc.ini 中的代码是(相应地填写您的服务器和数据库详细信息): [mssql_odbc] Description = MSSQL Server Driver = FreeTDS Database = Server = Port = 1433

然后我在需要连接到 SQL Server 数据库的域目录中创建了另一个文件,并尝试使用代码建立连接:

// Replace the value's of these variables with your own data:
$dsn = "mssql_odbc"; // Data Source Name (DSN) from the file /odbc.ini
$user = "";     // MSSQL database user
$password = ""; // MSSQL user password

$connect = odbc_connect($dsn, $user, $password);

//Verify connection
if ($connect) {
     echo "Connection established.";
     odbc_close($connect);
} else {
     die("Connection could not be established.");
}

它成功了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多