[己解决]ORM的问题求助

我在执行这句代码的时候会抛下面的异常

$towers = Tower::model()->findAll("city='$_GET[city]' AND status=0");

Description

CDbConnection failed to open the DB connection: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?' at line 1

Source File

/www/wwwroot/framework/db/CDbConnection.php(233)

00221:        {

00222:            if(empty($this->connectionString))

00223:                throw new CDbException(Yii::t('yii','CDbConnection.connectionString cannot be empty.'));

00224:            try

00225:            {

00226:                $this->_pdo=new PDO($this->connectionString,$this->username,

00227:                                    $this->password,$this->_attributes);

00228:                $this->initConnection($this->_pdo);

00229:                $this->_active=true;

00230:            }

00231:            catch(PDOException $e)

00232:            {

00233: throw new CDbException(Yii::t('yii','CDbConnection failed to open the DB connection: {error}',

00234:                    array('{error}'=>$e->getMessage())));

00235:            }

00236:        }

00237:    }

00238:

00239:    /**

00240:      * Closes the currently active DB connection.

00241:      * It does nothing if the connection is already closed.

00242:      */

00243:    protected function close()

00244:    {

00245:        $this->_pdo=null;

Stack Trace

#0 /www/wwwroot/framework/db/CDbConnection.php(208): CDbConnection->open()

#1 /www/wwwroot/framework/db/CDbConnection.php(187): CDbConnection->setActive(true)

#2 /www/wwwroot/framework/base/CApplication.php(734): CDbConnection->init()

#3 /www/wwwroot/framework/base/CApplication.php(385): CApplication->getComponent('db')

#4 /www/wwwroot/framework/db/ar/CActiveRecord.php(649): CApplication->getDb()

#5 /www/wwwroot/framework/db/ar/CActiveRecord.php(1695): CActiveRecord->getDbConnection()

#6 /www/wwwroot/framework/db/ar/CActiveRecord.php(512): CActiveRecordMetaData->__construct(Object(Tower))

#7 /www/wwwroot/tower/protected/models/Tower.php(11): CActiveRecord::model('Tower')

#8 /www/wwwroot/tower/protected/controllers/SiteController.php(21): Tower::model()

#9 /www/wwwroot/framework/web/actions/CInlineAction.php(32): SiteController->actionTower()

#10 /www/wwwroot/framework/web/CController.php(259): CInlineAction->run()

#11 /www/wwwroot/framework/web/CController.php(237): CController->runAction(Object(CInlineAction))

#12 /www/wwwroot/framework/web/CController.php(219): CController->runActionWithFilters(Object(CInlineAction), Array)

#13 /www/wwwroot/framework/web/CWebApplication.php(150): CController->run('tower')

#14 /www/wwwroot/framework/web/CWebApplication.php(121): CWebApplication->runController('site', 'tower')

#15 /www/wwwroot/framework/base/CApplication.php(170): CWebApplication->processRequest()

#16 /www/wwwroot/tower/index.php(11): CApplication->run()

#17 {main}

我的数据库配置如下:

'db'=>array(

            'connectionString'=>'mysql:host=192.168.0.100;dbname=database1',

		'username'=>'user',


		'password'=>'pass',

            'charset'=>'utf8',

            // turn on schema caching to improve performance

            'schemaCachingDuration'=>3600,

),

我已经找到解决方法:

参考了

http://www.yiiframew…52.html#msg2852

http://www.yiiframew…opic,407.0.html

设置CDbConnection的emulatePrepare属性为true即可

你这段代码危险:$towers = Tower::model()->findAll("city='$_GET[city]' AND status=0");

建议改成:



$towers = Tower::model()->findAll("city=:city AND status=0", array(':city'=>$_GET['city']));


好的,非常感谢