PHP中直接使用sql语句

SELECT * FROM `tf_user` WHERE (`company_id` = '117' OR `other_company_ids` LIKE '%117,%' OR `other_company_ids` LIKE '%,117,%' OR `other_company_ids` LIKE '%,117%') AND `id` != '37' AND `status` = '2' AND `role` = '2';

标准写法,关键字大写。
表名,字段名加`
值加'

PHP代码如下:

$sql = "SELECT * FROM `tf_user` WHERE (`company_id` = '".$user_info['company_id']."' OR `other_company_ids` LIKE '%".$user_info['company_id'].",%' OR `other_company_ids` LIKE '%,".$user_info['company_id'].",%' OR `other_company_ids` LIKE '%,".$user_info['company_id']."%') AND `id` != '".$this->user_info['id']."' AND `status` = '2' AND `role` = '2'";
$user_list = $user->query($sql);

原生sql的好处就是能够更加灵活的查询数据!也锻炼sql的能力!
但是一定要写的细心,不然不容易后期维护。

posted @ 2019-12-18 09:59  TBHacker  阅读(2413)  评论(0编辑  收藏  举报