PHP 5.3和sqlsrv_connect

时间:2014-12-03 13:28:32

标签: php

全部交易, 我有跟随问题:我想用PHP函数连接到ms sqlserver2008。 在过去,它适用于mssql_connect。 由于我已经更新到php 5.3,我有一个阵列问题:

语法:

$serverName = "sql server"; //serverName\instanceName, portNumber (default is 1433)
$connectionInfo = array( "db", "user", "pw");
$conn = sqlsrv_connect( $serverName, $connectionInfo);

if( $conn ) {
 echo "Connection established.<br />";
}else{
 echo "Connection could not be established.<br />";
 die( print_r( sqlsrv_errors(), true));
 }
 exit;

当我显示页面时,我收到此数组错误:

 Connection could not be established.
 Array ( [0] => Array ( [0] => IMSSP [SQLSTATE] => IMSSP [1] => -8 [code] => -8 [2] => An 
invalid connection option key type was received. Option key types must be strings. 
[message] => An invalid connection option key type was received. Option key types must be strings. ) ) 

也许有人有想法?为此,我将非常感激

3 个答案:

答案 0 :(得分:0)

检查 MANUAL 。 connectionInfo应该是键/值对数组:

$connectionInfo = array( "Database"=>"dbName", "UID"=>"userName", "PWD"=>"password");

答案 1 :(得分:0)

我认为这是数组定义,试试这个:

$connectionInfo = array( "Database"=>"db", "UID"=>"user", "PWD"=>"pw");

答案 2 :(得分:0)

你可能也会将参数括在双引号中。

语法错误:

  • $connectionInfo = array("Database"=>"$this->db_name", "UID"=>"$this->user_name", "PWD"=>"$this->user_pwd");
  • $connectionInfo = array("Database=>$this->db_name", "UID=>$this->user_name", "PWD=>$this->user_pwd");

更正语法:

$connectionInfo = array("Database"=>$this->db_name, "UID"=>$this->user_name, "PWD"=>$this->user_pwd);

完整代码应如下

<?php
class db_config{
private $server_name = "BITCODES\SQLEXPRESS";
private $user_name = "ERPSA";
private $user_pwd = "ERP@123";
private $db_name = "CANE_1718";
private $connectionInfo;
function db_connect(){ 
    $this->connectionInfo = array("Database"=>$this->db_name, "UID"=>$this->user_name, "PWD"=>$this->user_pwd);

    try{
        $sql_connect = sqlsrv_connect($this->server_name,$this->connectionInfo);
        if($sql_connect){
            echo 'Server Connected';
        }
        else{
            die(print_r(sqlsrv_errors()));
        }
    }
    catch(Exception $x){
        print_r($x);
    }

}

}