Esajay Yogal
Esajay Yogal

Reputation: 1

Can core php mysql function be used in Yii Framework?

Can we use core PHP functions in Yii framework?

Here, I have a core php function

function abc_function(){
$query=mysql_query("select emp_id, days from tmp_emp_work where comp_id='$comp_id' AND active=1");
$count=mysql_num_rows($query);
if ($count) {
    $del_existing=mysql_query("DELETE from temp_t_balances where com_id='$comp_id'");
    $row=mysql_fetch_array($query);
    while ($row) {
        $emp_id=$row['emp_id'];
        $array=dis_t_bal ($emp_id,$com_id);
        $start_bal=$array[0];
        $taken=$array[1];
        $comp_days=$array[2];
        $remain_bal=$array[3];
        $booked=$array[4];
        $true_bal=$array[5];
        $all_days=$array[6];
        $insert_bal=mysql_query("INSERT into temp_t_bal values ('','$comp_id','$emp_id','$start_bal','$taken','$remain_bal','$booked','$true_bal')");
        $row=mysql_fetch_array($query);
    }
}
}

Is this possible? Thanks in advance

Upvotes: 0

Views: 638

Answers (1)

crafter
crafter

Reputation: 6297

You have read all the comments about why you shouldn't do it. That's for you to evaluate.

However, the ability to submit queries directly are supported.

As an example

$sqlQuery = "SELECT employee_id, holidays
               FROM employee_work
              WHERE company_id='$company_id' AND emp_active=1';

$listEmployee = Yii::app()->db->createCommand($sqlQuery)->queryAll();


foreach ($listEmployee as $itemEmployee){
   print_r($itemEmployee);
}

Note that the above query is not safe, and can be subject to SQL injection. Yii offers a solution to upgrade the query to a safer format by using binding.

$listEmployee = Yii::app()->db->createCommand("SELECT employee_id, holidays
                                                 FROM employee_work
                                                WHERE company_id=:company_id
                                                  AND emp_active=1')
                              ->bindValue(':company_id',$company_id)
                              ->queryAll();

You also have access to the query() method for queries that do not return a resultset

Yii::app()->db->createCommand('DELETE * FROM customer')->query();

References : http://www.yiiframework.com/doc/guide/1.1/en/database.dao

Upvotes: 1

Related Questions