简体   繁体   English

如何使用sqlsrv api连接到PHP中的MSSQL?

[英]How to connect to MSSQL in PHP using sqlsrv api?

I am trying to connect to MSSQL in PHP using sqlsrv api. 我正在尝试使用sqlsrv api连接到PHP中的MSSQL。 When I run phpinfo() , it shows an a section for sqlsrv . 当我运行phpinfo()时 ,它显示sqlsrv的一部分 But while actually writing code, I cannot connect to MSSQL. 但是在实际编写代码时,我无法连接到MSSQL。 I am using SQL Server R2. 我正在使用SQL Server R2。 I connect to the database from the Management Studio using the specified hostname. 我使用指定的主机名从Management Studio连接到数据库。 Here is the code: 这是代码:

<?php
        $serverName = "TEST-PC\SQLEXPRESS";
        $databaseName = "TestDB";

        $connectionInfo = array("Database"=>"$databaseName");
        $conn = sqlsrv_connect( $serverName, $connectionInfo);
        if( $conn === false ){
            echo "Could not connect.\n";
            die( print_r( sqlsrv_errors(), true));
        }
        else{
            echo "Connection created";
        }
        $tsql = "SELECT * FROM Test";
        $result = sqlsrv_query($conn, $tsql);
        echo "Rows returned: ".sqlsrv_num_rows($result);
        sqlsrv_close();
    ?>

The code prints NOTHING on the page, not even any error message. 该代码在页面上没有打印任何内容,甚至没有任何错误消息。 Here is the entries in php.ini file: 这是php.ini文件中的条目:

extension=php_sqlsrv_53_ts_vc9.dll
extension=php_pdo_sqlsrv_53_ts_vc9.dll

Any help please? 有什么帮助吗?

Since you did not pass a user or password, the driver is trying to connect via Windows Authentication. 由于您未通过用户或密码,因此驱动程序正尝试通过Windows身份验证进行连接。 By running PHP from IIS, it will connect with the credentials of what whatever user IIS is running under (such as NT AUTHORITY\\IUSR). 通过从IIS运行PHP,它将与运行IIS的任何用户(例如NT AUTHORITY \\ IUSR)的凭据连接。 You need to add the IIS user to the list of allowed logins to SQL Server, and then add a user associated with that login to the database. 您需要将IIS用户添加到允许的SQL Server登录列表中,然后将与该登录关联的用户添加到数据库中。

It is because sqlsrv_query() uses SQLSRV_CURSOR_FORWARD cursor type by default. 这是因为sqlsrv_query()默认情况下使用SQLSRV_CURSOR_FORWARD游标类型。 However, in order to get a result from sqlsrv_num_rows(), you should choose one of these cursor types below: 但是,为了从sqlsrv_num_rows()获得结果,您应该在下面选择以下游标类型之一:

SQLSRV_CURSOR_STATIC SQLSRV_CURSOR_KEYSET SQLSRV_CURSOR_CLIENT_BUFFERED SQLSRV_CURSOR_STATIC SQLSRV_CURSOR_KEYSET SQLSRV_CURSOR_CLIENT_BUFFERED

For more information, check: Cursor Types (SQLSRV Driver) 有关更多信息,请检查: 游标类型(SQLSRV驱动程序)

In conclusion, Try this Code: 最后,请尝试以下代码:

$tsql=sqlsrv_query("SELECT * FROM Test"); $row_count = sqlsrv_num_rows($tsql); echo $row_count;

Did you mean to put "$connectionOptions" where you have "$connectionOptions"? 您是否打算将“ $ connectionOptions”放在您有“ $ connectionOptions”的位置?

Turn on error_reporting for php for more verbose output. 打开php的error_reporting可获得更详细的输出。

[EDIT] [编辑]

A few possibile solutions: 一些可能的解决方案:

  1. Check your Connection String properties for the MSSQL Database you're connecting to (ie are you matching the values exactly, or can you use (local) 检查您要连接的MSSQL数据库的连接字符串属性(即,您是完全匹配值还是可以使用(本地))
  2. It's possible the User you're logged in on doesn't have proper access to the database. 您登录的用户可能没有适当的数据库访问权限。 That sounds like an oversimplification, but check out your IIS settings, as well as FastCGI.impersonate settings. 这听起来似乎过于简单,但是请检查您的IIS设置以及FastCGI.impersonate设置。 Turn on IIS Windows Authentication settings in IIS, and fastcgi.impersonate = 1. There's a thorough article about this potential issue here: http://blogs.msdn.com/b/brian_swan/archive/2010/02/10/sql-server-driver-for-php-understanding-windows-authentication.aspx 在IIS中打开IIS Windows身份验证设置,并且fastcgi.impersonate =1。此处有关于此潜在问题的详尽文章: http : //blogs.msdn.com/b/brian_swan/archive/2010/02/10/sql-服务器驱动程序的php了解Windows-authentication.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM