Today try to access MS SQL database in Wamp environment, have the following experience. Using the PHP 5.3 + PDO_ODBC + YII 1.1 framework, the following two code snippets can be successful.
1, directly using the PDO method, you can obtain data.
1 $sql= "Select TOP ten [Name],[stcd],[tm],[pa] from At_pa";2 $CNX=NewPDO ("Odbc:driver={sql Server}; Server=10.5.1.47;database=mydb; ", ' username ', ' pass ');3 Var_dump($CNX);4 $rs=$CNX->query ($sql);5 Var_dump($rs);6 foreach($rs as $b) {7 Var_dump($b);8}
2. If you configure the DB attribute in the configuration file, you can use the Cdbcommand class to query the data.
Configuration Code return Array (' components ' = = Array (' db ' = = Array (' drivername ' = ' mssql ', ' ConnectionString ' = ' odbc:driver={sql Server}; server=127.0.0.1;database=mydatabase; ', ' username ' = ' username ', ' password ' = ' pass ',) ,);// Query Code $sql = "Select TOP ten [Name],[stcd],[tm],[pa] from At_pa"; $rs = Yii::app ()->db->createcommand ($sql) Queryall (); Var_dump ($rs); foreach ($rs as $b) { var_dump ($b);}
3, the most exciting step, the use of Cactiverecord, failed.
If you query the names of all tables,
$rs = Yii::app ()->dbhuaiwater->schema->tablenames;
You can get all the names of the data tables. However, the GII cannot generate the model automatically, always prompting that the specified table does not exist. You cannot perform a data query even if you manually edit the model subclass.
Attempt to access MS SQL using the PHP framework Yii