0

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 t.week_id=w.id"; 

    $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?

1
  • Do you need the query string in JSON format? Or do you just want it as plain string? Commented May 28, 2014 at 8:48

3 Answers 3

3

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(
    'id'=>'grid-id',
     '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()

Sign up to request clarification or add additional context in comments.

2 Comments

Could also work with a CArrayDataProvider built with the results of the query, although CSqlDataProvider is indeed the most straight-forward solution of both
Your use of queryScalar() is incorrect, check docs
0

Use Mysql_fetch _array

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 t.week_id=w.id"; 

        $query = mysqli_query($datalogin, $sql);
        while($row = mysqli_fetch_array($query)){
        echo $row;
        }

    }

1 Comment

I need to return it into the view instead of echo , and it returns an array rather than string.
0

Assuming from your qu. that you want the week number and start date as one string, you have to concatenate the two columns in the sql.

You also need to specify that the week number is from the row with the maximum start date, which isn't as simple as you might first think.

I don't like injecting the person_id straight into SQL, it isn't awful in this case but is a bad habit to get into security-wise. There are binding methods available in the framework and I agree with Arek, that you should lean on the yii framework as much as possible.

To get the scalar string value, if you are insisting on using your own SQL.. I suggest the following:

$sql='
  SELECT CONCAT('Week ',tw.number,' starting ',tw.start_date)
    FROM tbl_week tw
    JOIN (
      SELECT MAX(twi.start_date) max_start_date
        FROM tbl_week twi
        JOIN tbl_person_week tpwi
          ON tpwi.week_id = twi.id
         AND tpwi.person_id = :person_id
         ) i
      ON tw.start_date = i.max_start_date;
';
$command=Yii::app()->db->createCommand($sql);
$command->bindParam(":person_id", $this->id);
return $command->queryScalar();   

3 Comments

I see. looks more complicated that I first thought. I tried using your code , but I get the white screen of death :)
Nope , completely blank.
There may be something wrong with the db config.. you should have an application.log file on your webserver with a new error in it too. It'll be pretty hard to dev and debug without access to the stack-traces.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.