【问题标题】:PHP Connection with SQL Server 2008 in Windows10在 Windows 10 中与 SQL Server 2008 的 PHP 连接
【发布时间】:2015-09-17 13:29:57
【问题描述】:

我正在尝试将 PHP 与 SQL Server 2008 连接,但无法建立连接。

我遵循了以下教程: https://technet.microsoft.com/en-us/library/cc793139(v=sql.90).aspx

我的 PHP.ini

我的 Dll 和位置:

我的 PHP 版本:

我的扩展显示在 WAMP PHP 扩展中:

但是当我尝试以下 PHP 代码时:

<?php
$serverName = "SAPSRV"; //serverName\instanceName

// Since UID and PWD are not specified in the $connectionInfo array,
// The connection will be attempted using Windows Authentication.
$connectionInfo = array( "Database"=>"SmartLogistic");
$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));
}
?>

我仍然收到以下错误:

( ! ) 致命错误:调用未定义的函数 sqlsrv_connect() H:\wamp\www\job\sql.php

帮助我解决问题,在此先感谢..

【问题讨论】:

  • @PaulCrovella 我想确保我做的一切都是正确的..所以给别人更多的选择来使用图像找出我的错也不错,所以只有我试过..

标签: php sql-server sql-server-2008


【解决方案1】:

几个月前我遇到了完全相同的错误,这对我有用。

首先要使 sqlsrv 驱动程序工作,您需要 32 位 WAMP 服务器,它不适用于 64 位 WAMP。

我使用了 php_sqlsrv_55_ts DLL 文件,我可以在 WAMP 上的 PHP 中运行我的查询。

现在这里是有趣的地方。我假设您将在基于 Linux 的服务器上运行您的系统?

如果是这种情况,sqlsrv 不能在基于 Linux 的操作系统上运行,因此您必须在 Linux 操作系统上使用 mssql 驱动程序。 Here is a link 进行设置。

最后,您需要编写一个自定义函数来检查您的 PHP 扩展,然后根据您运行它们的位置选择正确的驱动程序。

这是我创建的一个函数,用于查看加载了哪个驱动器并确定要使用哪个驱动器。

// Function to check which MS SQL database driver is loaded
function get_db_loaded_extension() {
    // Assign php loaded extensions to an array variable
    $php_loaded_extensions_array = get_loaded_extensions();

    // Loop through each php extension in the array
    foreach($php_loaded_extensions_array as $php_ext) {
        // Switch to check which MS SQL database driver is loaded
        switch($php_ext) {
            case "mssql":
                $return = "mssql";
                break;

            case "sqlsrv":
                $return = "sqlsrv";
                break;
        }
    }

    // Check if a MS SQL database driver have been found
    if(!isset($return)) {
        $return = js_dialog("No Microsoft SQL database driver loaded.");
    }

    return $return;
}

作为示例,这里还介绍了如何创建连接。

// Call function to check which MS SQL database driver extension is loaded
$mssql_db_driver = get_db_loaded_extension();

// Switch to determine how to make the appropriate connection
switch($mssql_db_driver) {
    case "mssql":
        // Set the MSSQL database variables
        $mssql_servername = "xxx.xxx.xxx.xxx";
        $mssql_username = "my_user";
        $mssql_password = "**********";

        // Create conection to MSSQL using the mssql php extension
        $mssql_conn = mssql_connect($mssql_servername, $mssql_username, $mssql_password);
        break;

    case "sqlsrv":
        // Set the MSSQL database variables
        $mssql_servername = "xxx.xxx.xxx.xxx";
        $mssql_conn_info = array(
          "UID" => "my_user",
          "PWD"=> "**********"
        );

        // Create conection to MSSQL using the sqlsrv php extension
        $mssql_conn = sqlsrv_connect($mssql_servername, $mssql_conn_info);
        break;

    default:
        echo $mssql_db_driver;
        break;
}

// Check the MSSQL connection
if(!$mssql_conn) {
    exit("Could not connect to the database. Please contact the administrator.");
}

【讨论】:

    【解决方案2】:

    看起来是 php 而不是 sql 存在问题。 sqlsrv_connect 是一个未知函数,因此无法调用。尝试使用 PDO 开始。它是一种更好、更安全的与 SQL 数据库交互的方式。
    http://php.net/manual/en/class.pdo.php

    以下应该允许您查询 sql server db。

    $dbh = new PDO ("dblib:host=$hostname:$port;dbname=$dbname","$username","$pw");
    $stmt = $dbh->prepare("SELECT * FROM Table");
    $stmt->execute();
    

    【讨论】:

    • ( ! ) 致命错误:未捕获的异常 'PDOException' 并在第 7 行的 H:\wamp\www\job\sql.php 中显示消息 'could not find driver' (!) PDOException: could not在第 7 行的 H:\wamp\www\job\sql.php 中找到驱动程序
    • 我尝试了你的代码并收到了上述错误。
    • 您缺少 sql server 驱动程序。 microsoft.com/en-IE/download/details.aspx?id=20098
    • @DanHastings,对。因此,为什么问题中的所有屏幕截图都是询问者尝试安装它们的原因。
    • 您得到的异常会暗示其他情况。确保您的系统上没有 2 个版本的 PHP。我可以看到您已经安装了 mysql 驱动程序,但是您的代码无法找到它们。运行 phpinfo() 以查看您的代码能够获取哪些驱动程序。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-27
    • 1970-01-01
    • 2021-08-29
    • 1970-01-01
    相关资源
    最近更新 更多