最新消息:觉得本站不错的话 记得收藏哦 博客内某些功能仅供测试 讨论群:135931704 快养不起小站了 各位有闲钱就打赏下把 My Email weicots#gmail.com Please replace # with @

PHP 使用 SQL Server 查询 以及常见错误

MSSQL ajiang-tuzi 2424浏览

一些常见的错误

HResult 0xFFFFFFFF,级别 16,状态 1
SQL Server Network Interfaces: An error occurred while obtaining the dedicated administrator connection (DAC) port. Make sure that SQL Browser is running, or check the error log for the port number [xFFFFFFFF].
Sqlcmd: 错误: Microsoft SQL Server Native Client 10.0 : A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online.。
Sqlcmd: 错误: Microsoft SQL Server Native Client 10.0 : Login timeout expired。

Error Locating Server/Instance Specified [xFFFFFFFF]”. SQLSRV32 to the rescue 错误参考
Incorrect instance name:

C:\Users\maspeng>sqlcmd -S SpikeSrv2008\Spike2009
HResult 0xFFFFFFFF, Level 16, State 1
SQL Server Network Interfaces: Error Locating Server/Instance Specified [xFFFFFFFF].
Sqlcmd: Error: Microsoft SQL Server Native Client 10.0 : A network-related or instance-specific error has occurred while establishing a connection to SQL Server.
Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections.
 

As we can see, the errors are identical. However, this is an ideal situation since we know that either the server name or the instance name is wrong.
But if you are not sure which one is incorrect (we are assuming that we know that the server is up and running and can be connected to from other machines).

What you can do to get a bit closer to what is happening is to create an ODBC connection to the server that you cannot connect to.
Click “Start”, select “Run” and type odbcad32.exe or run it directly from C:\Windows\System32\odbcad32.exe

Select “Add” (for a new DSN) then select the Sqlsrv32 driver (“SQL Server”) not the sqlncli or sqlncli10 driver (these are the newer drivers and will give the same error as above)
Then enter anything in the Name textbox and then enter the info for the Server in that textbox.

In the case of the incorrect server name (SpikeSrv2009\Spike2008):

我写的一个简易的链接 基类

<?php
/**
 * WeiCot Framework
 * User: jiang
 * Date: 2016/10/15
 * Time: 17:16
 * 数据库操作简易基类 MsSql
 */
namespace Weicot;
class MsSql
{

    static $serverName;
    static $connectionInfo;
    static $con;

    /**
     * 初始化链接
     */
    static function ini()
    {
        self::$serverName = "serverName\sqlexpress"; //本地链接
        self::$connectionInfo = array("Database" => "Data1");
        self::$con = sqlsrv_connect(self::$serverName, self::$connectionInfo);
        if (!self::$con) {
            throw new \Exception("Connection could not be established" . print_r(sqlsrv_errors(),true));
        }
    }

    /***
     * @param $sql
     * @return bool|resource
     * 执行sql
     */
    static  public function exe($sql)
    {   self::ini();
        $info = sqlsrv_query(self::$con, $sql);
        if ($info === false) {
            throw new \Exception("QUERY ERROR" . print_r(sqlsrv_errors(),true));
        }
        return $info;
    }

    /***
     * @param $sql
     * @return array|null
     * 获取mssql 数据
     */
    static public function getData($sql){
        $DataRouse=self::exe($sql);
        if(sqlsrv_has_rows($DataRouse))
        {  
            $rowData["row"]= sqlsrv_num_rows($DataRouse);
            while( $row = sqlsrv_fetch_array($DataRouse, SQLSRV_FETCH_ASSOC))
            {
                $rowData[]=$row;
            }
            return $rowData;
        }
        else
        {
            return NULL;
        }

    }



}
//使用方法
var_dump(MsSql::getData("SELECT ID, Num FROM Data3.dbo.Data3All WHERE Num = '72'"));

参考资料
使用 PHP 链接 SQL Server
认识SQLServer索引以及单列索引和多列索引的区别

转载请注明:(●--●) Hello.My Weicot » PHP 使用 SQL Server 查询 以及常见错误