2015-07-20 16:16:27 3539次浏览 3条回答 0 悬赏 0 金钱

Yii版本:1.1.17-dev

$connection=new CDbConnection('mysql:host=localhost;dbname=test');
$sql='select * from {{user}}';
$res=$connection->createCommand($sql)->queryAll();

配置里面也加了tablePrefix

'connectionString' => 'mysql:host=localhost;dbname=test',
'emulatePrepare' => true,
'username' => 'root',
'password' => '123456',
'charset' => 'utf8',
'tablePrefix' => 'tbl_',

运行后提示SQL语法错误:

CDbCommand failed to execute the SQL statement: 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 '{user}}' at line 1. The SQL statement executed was: select * from {{user}} 
  • 回答于 2015-07-20 16:52 举报

    表前面还要加百分号

    3 条回复
    回复于 2015-07-22 14:35 回复

    真坑,文档里面没有百分号

    回复于 2015-07-22 14:44 回复

    不行,还是报错

    回复于 2015-07-22 16:31 回复

    那你就不要这么写在你表前面加上表前缀就可以了,把括号都去掉

    觉得很赞
  • 回答于 2015-07-21 10:26 举报

    {{%user}}应该这样的。

    4 条回复
    回复于 2015-07-23 07:51 回复

    1版本的使用不需要%吧?2版本才多出个%

    回复于 2015-07-23 08:42 回复

    是的

    回复于 2015-07-23 09:53 回复

    既然是,人家指明是1版本,而且代码也是1版本,你干嘛用2版本来回答

    回复于 2015-07-23 17:55 回复

    惯性思维了,平常都是用2版本。

  • 回答于 2015-07-23 08:08 举报

    你还需要在model中修改tablename属性。
    比如你的User model中,将

    public function tableName()
    {
      	return 'user';
    }
    

    修改为:

    public function tableName()
    {
    	return '{{user}}';
    }
    
您需要登录后才可以回答。登录 | 立即注册
qf19910623
试用期

qf19910623

注册时间:2015-07-20
最后登录:2020-01-17
在线时长:2小时2分
  • 粉丝0
  • 金钱10
  • 威望0
  • 积分30

热门问题