SQL Server not Connecting nor Tossing Errors

自闭症网瘾萝莉.ら 提交于 2019-12-12 03:42:37

问题


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:


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";
    ?>


来源:https://stackoverflow.com/questions/40899323/sql-server-not-connecting-nor-tossing-errors

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!