1

我写了一个类来将 php 页面连接到 Microsoft SQL Server 2008

源代码:

class DBManager{
    public static $server ="192.168.1.79";
    public static $database="db";
    public static $username ="user";
    public static $password ="pass";

    public function __constructor(){
}


    public function ricavaConcessionarioRicercato($codconc,$concessionario,$cap){
        include_once("Concessionario.php");
        $listaConcessionariTrovati = array();
        $connection = mssql_connect($server,$username,$password);
        mssql_select_db($database)or die('MSSQL error' . mssql_get_last_message());

        $querySQL = "SELECT DISTINCT [CodConc]
                                ,[Concessionario]
                                ,[Indirizzo]
                                ,[Mail]
                                FROM [Conserva_Pg].[dbo].[PLTWConcGestCAP]
                                WHERE CodConc LIKE '%$codconc%' AND Concessionario LIKE '%$concessionario%'". ($cap == "") ?"": "AND CAPdiGest LIKE '%$cap%'";
        $risultato = mssql_query( $querySQL);
        while($row = mssql_fetch_object($risultato)){
            $concessionario = new Concessionario();
            $concessionario->codConc = $row->CodConc;
            $concessionario->concessionario = $row->Concessionario;
            $concessionario->indirizzo = $row->Indirizzo;
            $concessionario->email = $row->Mail;
            $listaConcessionariTrovati[] = $concessionario;

        }
        mssql_free_result($risultato);
        mssql_close($connection);
        return $listaConcessionariTrovati;
    }

}

但是当我执行该功能时,我收到此警告:

警告:mssql_select_db() [function.mssql-select-db]:无法选择数据库:在 C:\Applicativi\P&G\GestioneConcessionari\DBManager.php 中第 21 行 MSSQL 错误将数据库上下文更改为“主”。

之后,我使用 ADODB 连接重写了函数:

class DBManager{

    public function __constructor(){


    }



    public function ricavaConcessionarioRicercato($codconc,$concessionario,$cap){
        include_once("Concessionario.php");
        $connSQL = new COM("ADODB.Connection"); 
        $connstr = "DRIVER={SQL Server};"; 
        $connstr .= "SERVER=192.168.1.79;DATABASE=db;uid=user;pwd=pass;"; 
        $connSQL->open($connstr); 
        $listaConcessionariTrovati = array();


        $queryconc = "SELECT DISTINCT [CodConc]
                                ,[Concessionario]
                                ,[Indirizzo]
                                ,[Mail]
                                FROM [Conserva_Pg].[dbo].[PLTWConcGestCAP]
                                WHERE CodConc LIKE '%$codconc%' AND Concessionario LIKE '%".$concessionario."%'";


        $rsta = $connSQL->execute($queryconc);
        if(!$rsta->eof()){
        $rsta->movefirst();
            while(!$rsta->EOF){
                $concessionario = new Concessionario();
                $concessionario->codConc = $rsta['CodConc']->value;
                $concessionario->concessionario =  $rsta['Concessionario']->value;
                $concessionario->indirizzo =  $rsta['Indirizzo']->value;
                $concessionario->email =  $rsta['Mail']->value;
                $listaConcessionariTrovati[] = $concessionario;
                $rsta->movenext();
            }
        }
        $rsta->Close();
        $connSQL->Close();
        $rsta = null;
        $connSQL = null;
        print_r($listaConcessionariTrovati);
        return $listaConcessionariTrovati;
    }
}

该脚本正常运行!为什么????

非常感谢..

此致

4

1 回答 1

2

使用时必须指定连接mssql_select_db()

mssql_select_db($database, $connection);
于 2013-10-11T07:56:51.140 回答