SQL Server& Php无法使用

时间:2013-10-11 07:53:46

标签: php sql-server

我写了一个类来将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]:无法选择数据库:在第21行的C:\ Applicativi \ P& G \ GestioneConcessionari \ DBManager.php中   MSSQL errorChanged数据库上下文为'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;
    }
}

此脚本正常运行! WHY ????

非常感谢..

最好的问候

1 个答案:

答案 0 :(得分:2)

使用mssql_select_db()

时必须指定连接
mssql_select_db($database, $connection);