2014-05-28 08:40
浏览 7


I'm using SQL in Yii framework. I need to show the person's latest active week (it's number and date).So I wrote following code:

public function latestWeek() 
    $datalogin=//the login is working fine
    $sql ="SELECT w.number,MAX(w.start_date)
    FROM tbl_person_week t, tbl_week w
    WHERE t.person_id=$this->id AND"; 

    $query = mysqli_query($datalogin, $sql);
    return $query;

Now , I checked this query on the server and it works fine (almost) but first thing: I need to convert it into string , because yii's CgridView can't read it , and I couldn't find a working solution for this.

Second: on the server , it gave me the max date indeed , but not it's correct number , but the first number available. How can I fix this as well?

图片转代码服务由CSDN问答提供 功能建议

我在 Yii framework 中使用 SQL 。\ 我需要显示该人的最新活动周(它的数量和日期)。所以我写了以下代码:

  public function latestWeek()
 $ datalogin = /  /登录工作正常
 $ sql =“SELECT w.number,MAX(w.start_date)
 FROM tbl_person_week t,tbl_week w 
 WHERE t.person_id = $ this-> id AND t.week_id = w  。ID”;  
 $ query = mysqli_query($ datalogin,$ sql); 
返回$ query; 

现在,我检查了这个查询 服务器,它工作正常(几乎),但第一件事:我需要将其转换为字符串,因为yii的CgridView无法读取它,我找不到一个有效的解决方案。 < p>第二:在服务器上,它确实给了我最大日期,但不是正确的数字,而是第一个可用数字。 我该如何解决这个问题呢?

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

3条回答 默认 最新

  • dqvy87517 2014-05-28 10:10

    Queries like that should never be used in objective framework. If yu want to execute your own query, you should do it this way:

    $sql = "your sql code";
    $array = Yii::app()->db->createCommand($sql)->queryAll();

    As result you will get multidimensional array with selected columns and rows

    If you want to use it in grid view, you should do it this way:

    $count = Yii::app()->db->createCommand($sql)->queryScalar();
    $dataProvider = new CSqlDataProvider($sql, array('totalItemCount'=>$count));
    $this->widget('zii.widgets.grid.CGridView', array(
         'dataProvider'=> $dataProvider,

    You can also use connection other than Yii::app()->db. Check CDbConnection class in docs.

    edit: if you wanna use queries like mysql_fetch_assoc, check out also queryRow() method instead of queryAll()

    解决 无用
    打赏 举报