Search code examples
phpmysqlyii2yii2-advanced-appyii2-model

How to use query data for another sql query in yii2


I have a query here:

  public function SearchExactJob($id) {
      $connect = \Yii::$app->db;

      $query = $connect->createCommand('
           SELECT job_name FROM job_questions WHERE job_id=:id')
           ->bindValue(':id':$id)
           ->queryAll();

}

I want to use the job_name for another query: PS: I just wrote this down on a paper in case i'll be able to find out how to get this data

  $anotherquery = $connect->createCommand(
                  'SELECT * FROM company_questions
                   WHERE company_question = [the job_name I want to get from the `$query`])

Also if I will successfully get the $anotherquery work, I want to get the data and pass it on the view assigned to this.

But then, im asking for help because I just recently used Yii2. Please help me.


Solution

  • I've solved this one.

         $query=$connect->createCommand('
           SELECT job_name FROM job_questions WHERE job_id=:id')
           ->bindValue(':id':$id)
           ->queryOne(\PDO::FETCH_OBJ);
    

    add the data on a variable: ex. $job_name

    $job_name = $query->job_name;