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 エラーデータベース コンテキストを 'master' に変更しました。

その後、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 に答える