通过php登录my sql server,PHP 使用 SQL Server 查询 以及常见错误

一些常见的错误

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。

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):

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

/**

* 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'"));

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值