dongtaigan1594 2018-12-12 03:59
浏览 271
已采纳

无法连接到mssql服务器或sqlsrv没有显示在phpinfo上

i have problem to connect to server, i did all the step from here and i already change php.ini On windows: extension_dir = "D:\xampp\php\ext" but i still cant connect, My PHP Version is 7.2.11 i tried with mssql_connect() and sqlsrv_connect()

here my

`<?php
$servername = "1111";
$username = "user";
$password = "123";
$dbname = "DEV";
$connection = mssql_connect($servername, $username, $password);
if (!$connection) {  die('Not connected : ' . mssql_get_last_message());} 
$db_selected = mssql_select_db($dbname, $connection);
if (!$db_selected) {
  die ('Can\'t use db : ' . mssql_get_last_message());
} else{
echo "success";} ?>`
  • 写回答

1条回答 默认 最新

  • dongzhuo5185 2018-12-12 06:51
    关注

    MSSQL extension for PHP (mssql_ functions) and PHP Driver for SQL Server (sqlsrv_ functions) are two different extensions for PHP.

    MSSQL extension is not available anymore on Windows with PHP 5.3 or later, so you need to install PHP Driver for SQL Server. You need to download appropriate version of this driver. For PHP 7.2 - version 5.2 or 5.3 (32-bit or 64-bit also depends on PHP version). Also download and install an appropriate ODBC driver.

    Check the configuration with <?php phpinfo();?>. There should be a section with name pdo_sqlsrv (if you use PDO) and/or sqlsrv (without PDO).

    Example:

    <?php
    $server   = '1111';
    $database = 'DEV';
    $uid      = 'user';
    $pwd      = '123';
    
    # SQL Server authentication
    #$cinfo = array(
    #    "Database" => $database,
    #    "UID" => $uid,
    #    "PWD" => $pwd
    #);
    
    # Windows authentication
    $cinfo = array(
        "Database" => $database
    );
    
    $conn = sqlsrv_connect($server, $cinfo);
    if ($conn === false) {
        echo "Error (sqlsrv_connect): ".print_r(sqlsrv_errors(), true);
        exit;
    } else {
        echo "success";
    }
    
    sqlsrv_close($conn);
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?