简体   繁体   English

可以在Yii Framework中使用核心php mysql函数吗?

[英]Can core php mysql function be used in Yii Framework?

Can we use core PHP functions in Yii framework? 我们可以在Yii框架中使用核心PHP函数吗?

Here, I have a core php function 在这里,我有一个核心的php函数

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 提前致谢

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. 请注意,以上查询并不安全,并且可能会受到SQL注入的影响。 Yii offers a solution to upgrade the query to a safer format by using binding. Yii提供了一种通过使用绑定将查询升级到更安全格式的解决方案。

$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 对于不返回结果集的查询,您还可以访问query()方法

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

References : http://www.yiiframework.com/doc/guide/1.1/en/database.dao 参考资料: http : //www.yiiframework.com/doc/guide/1.1/en/database.dao

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM