Attempt to access MS SQL using the PHP framework Yii

Source: Internet
Author: User
Tags php framework sql using yii

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.