douzhi8488 2016-11-30 22:20
浏览 56
已采纳

SQL Server没有连接也没有抛出错误

I am attempting to connect to an already established database here at work. We don't have access to the back-end, but we do have the server address and the login information to connect to it - I was able to connect last year using PDO and dblib, but dblib is no longer supported in PHP 7 so I am attempting to use the sql server driver.

I set up a basic connection string to test it and am not getting any specified errors, nor am I getting any results:

<?php
    //Display All Errors
    ini_set('display_errors', 1);
    ini_set('display_startup_errors', 1);
    error_reporting(E_ALL);

    echo "Test1";

//Try the Connection
try {
    //Connection Variables
    $server = "11.123.123.123, 1234";
    $database = 'myDatabase';
    $username = "myUsername";
    $password = "myPassword";

    //Connection String
    $conn = new PDO ("sqlsrv:Server=$server;Database=$database","$username","$password");

    //Initiating Error Detection
    $conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );   
}  

//Catch exceptions to the Try
catch(Exception $e)  {   
    echo "Test3";
    die( print_r( $e->getMessage() ) );   
}  

//Prepare SQL Statement
$stmt = $conn->prepare("SELECT * FROM WorksheetPriceChangeData");

//Execute SQL Statement
$stmt->execute();
    while ($row = $stmt->fetch()) {
      print_r($row);
    }
    echo "Test4";
?>

(placeholders used for sensitive information). This code results in an empty page that displays the "Test1" echo at the top. There are no errors in my PHO log, phpinfo() shows that sqlsrv PDO and the sqlsrv extension (4.0.8629.3) are active, and if I use a bogus server address (like 11.123.123.123) it does the same thing.

I'll also add that I am on Windows Server 2012 R2 and the database is displayed in the Data Source Administrator. It connects successfully with the "With Windows NT authentication using the network login ID" checked and "Connect to SQL Server to obtain default settings for the additional configuration options" checked.

I would appreciate any direction or thoughts offered on this. It's difficult to proceed without even an error message or log.

Kind Regards, Ben David

  • 写回答

1条回答 默认 最新

  • dongqie5529 2016-12-07 22:12
    关注

    This Connection works on Windows Server 2012 R2 in PHP 5.X-7.X (The PHP version running in x86 architecture mode).

    On a fresh install in IIS you may first need to change the version of PHP to 5.X or 7.X (the x86 architecture version of 7.0 is necessary to handshake with the Sybase database we're working with) by going into >IIS Manager >PHP Manager and selecting "Change PHP Version". After this it may be neccesary to create an ODBC DSN by running the ODBC Data-Sources 32-bit executable and creating a connection called "myConnection" (if the name is different the variable will need to be adjusted below). This connection needs to be created with the SQL Anywhere 12 Driver. The connection will need to be created with a server name and database name which match the credentials of your database.

    Next go to IIS Manager> Authentication. Select "Anonymous Authentication", click "Edit" and make sure "Application Pool Identity" is checked.

    After this you will need to configure PHP to use the DSN by installing and initiating the odbc_pdo driver/extension. First make sure the php_pdo_odbc.dll file is in your /ext directory off of the main PHP directory of the version you are using (make sure you are making these adjustements in the "Program Files (X86)" folder, or else you are adjusting the now inactive x64 PHP version) - or if it is not there download it. Once the file is in the directory edit the php.ini file in the main PHP directory to include the following extension line in the extension block at the end of the file: extension=php_pdo_odbc.dll. Restart the computer and the odbc_pdo driver should now be enabled. The below connection script should now work and print an array blast of the system objects for that database.

        <?php       
            //---Display All Errors
            ini_set('display_errors', 1);
            ini_set('display_startup_errors', 1);
            error_reporting(E_ALL);
    
            echo "Boom! <br /><br />";
    
            //---Try the Connection
            try {
    
                //Connection Variables
                $dsn = "odbc:myConnection";
                $username = "myUsername";
                $password = "myPassword";
    
                //Connection String
                $conn = new PDO($dsn, $username, $password);
    
                //Initiating Error Detection
                $conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );   
            }
    
            //---Catch exceptions to the Try
            catch(Exception $e)  {   
                echo "Invalid Connection";
                //Print Error Messages
                die( print_r( $e->getMessage() ) );   
            }  
    
            //---SQL Statement(s)
            $stmt = $conn->prepare("SELECT * FROM sysobjects WHERE type = 'U'");
    
            //---Execute SQL Statement
            $stmt->execute();
                while ($row = $stmt->fetch()) {
                  print_r($row);
                    echo "<br />";
                }
    
            echo "<br />Connected Successfully";
        ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?