SQL Server没有连接也没有抛出错误 [英] SQL Server not Connecting nor Tossing Errors

查看:92
本文介绍了SQL Server没有连接也没有抛出错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试连接到这里已经建立的数据库.我们无权访问后端,但确实有服务器地址和登录信息可连接至后端-去年我可以使用PDO和dblib进行连接,但PHP 7不再支持dblib,因此我正在尝试使用sql服务器驱动程序.

我设置了一个基本的连接字符串来对其进行测试,但是没有得到任何指定的错误,也没有得到任何结果:

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

(用于敏感信息的占位符).此代码将导致一个空白页面,该页面在顶部显示"Test1"回显.我的PHO日志中没有错误,phpinfo()表明sqlsrv PDO和sqlsrv扩展名(4.0.8629.3)处于活动状态,并且如果我使用虚假的服务器地址(如11.123.123.123),它将执行相同的操作.

我还将添加我在Windows Server 2012 R2上的信息,并且该数据库将显示在数据源管理器"中.可以成功选中使用网络登录ID进行Windows NT身份验证"和连接到SQL Server以获取其他配置选项的默认设置".

对此,我将不胜感激.甚至没有错误消息或日志也很难进行.

亲切的问候, 本·戴维

解决方案

此连接可在Windows Server 2012 R2上以PHP 5.X-7.X(在x86架构模式下运行的PHP版本)运行.

在IIS中全新安装时,您可能需要先将PHP版本更改为5.X或7.X(与我们正在使用的Sybase数据库握手时,x86体系结构版本7.0是必需的)进入> IIS管理器> PHP管理器,然后选择更改PHP版本".此后,可能需要通过运行32位ODBC数据源可执行文件并创建一个名为"myConnection"的连接来创建ODBC DSN(如果名称不同,则需要在下面对变量进行调整).需要使用SQL Anywhere 12驱动程序创建此连接.需要使用与数据库凭据匹配的服务器名称和数据库名称来创建连接.

接下来转到IIS管理器>身份验证.选择匿名身份验证",单击编辑",并确保已选中应用程序池标识".

此后,您需要通过安装并启动odbc_pdo驱动程序/扩展程序来配置PHP以使用DSN.首先,请确保php_pdo_odbc.dll文件位于您使用的版本的主PHP目录下的/ext目录中(请确保在"Program Files(X86)"文件夹中进行了这些调整,否则您将进行调整) (目前处于非活动状态的x64 PHP版本)-如果没有,请下载该版本.将文件放入目录后,在主PHP目录中编辑php.ini文件,以在文件末尾的扩展块中包含以下扩展行:extension = php_pdo_odbc.dll.重新启动计算机,现在应该启用odbc_pdo驱动程序.现在,下面的连接脚本应该可以工作了,并为该数据库打印系统对象的数组爆炸.

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

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

解决方案

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

这篇关于SQL Server没有连接也没有抛出错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆