2016-04-07 14:05:47 53834次浏览 3条回答 5 悬赏 23 金钱
$connection  = Yii::$app->db;
$sql     = "select * from hd_article where aid<".$aid." order by aid desc limit 1";
$command = $connection->createCommand($sql);
$res     = $command->queryAll($sql);

通过这种查询,显示错误了!

  • 回答于 2016-04-07 14:13 举报

    错误呢,是PHP语法错误还是MYSQL错误

    5 条回复
    回复于 2016-04-07 14:26 回复
    Database Exception – yii\db\Exception
    
    PDOStatement::fetchAll() expects parameter 1 to be long, string given
    The SQL being executed was: select * from hd_article where aid<31 order by aid desc limit 1
    ↵
    Caused by: yii\base\ErrorException
    
    PDOStatement::fetchAll() expects parameter 1 to be long, string given
    
    in D:\WWW\web\vendor\yiisoft\yii2\db\Command.php at line 848
    
    回复于 2016-04-07 14:29 回复

    你limit 1 就不用queryAll 用queryOne

    回复于 2016-04-07 14:30 回复

    还是不行!

    回复于 2016-04-07 14:31 回复

    这样写是非常不安全的,有SQL注入的危险,还是建议用 连贯操作。

    回复于 2016-04-07 14:32 回复

    后面我绑定参数的形式来写!

  • 回答于 2016-04-07 16:15 举报
    1 条回复
    回复于 2016-04-07 17:56 回复

    嗯 已经搞定!多谢回答!

  • 回答于 2016-04-07 17:50 举报

    先创建连接对象

    $connection = new \yii\db\Connection([
        'dsn' => $dsn,
         'username' => $username,
         'password' => $password,
    ]);
    $connection->open();
    

    查询返回多行:

    $command = $connection->createCommand('SELECT * FROM post');
    $posts = $command->queryAll();
    

    返回单行:

    $command = $connection->createCommand('SELECT * FROM post WHERE id=1');
    $post = $command->queryOne();
    

    查询多行单值:

    $command = $connection->createCommand('SELECT title FROM post');
    $titles = $command->queryColumn();
    

    查询标量值/计算值:

    $command = $connection->createCommand('SELECT COUNT(*) FROM post');
    $postCount = $command->queryScalar();
    

    要养成先查文档的习惯:
    http://www.yiichina.com/doc/guide/2.0/db-dao

    2 条回复
    回复于 2016-04-07 17:55 回复

    万分感谢!好人呀。回答得这么如此透彻明了!

    回复于 2016-04-07 17:57 回复

    我只是把文档的内容复制过来给而已,平时要多看看文档

    , 觉得很赞
您需要登录后才可以回答。登录 | 立即注册
yltianmei
助理

yltianmei

注册时间:2015-05-09
最后登录:2016-05-10
在线时长:5小时50分
  • 粉丝5
  • 金钱142
  • 威望0
  • 积分192

热门问题