首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CodeIgniter 2.0.3中活动记录中的异常回退行为

CodeIgniter 2.0.3中活动记录中的异常回退行为
EN

Stack Overflow用户
提问于 2011-09-20 09:00:07
回答 8查看 10.7K关注 0票数 23

以前,在CI版本2.0中,我的所有查询都运行良好,但是当我升级到2.0.3时,我的一些SELECT查询被中断了。

CI会自动添加backticks (),但在旧版本中,它将按原样运行。

CI用户手册已指示在

db->select

作为

但还是没起作用。

守则如下:

代码语言:javascript
复制
class Company_model extends MY_Model
{

----------------

$this->db->select(' count('.$fieldname. ') as num_stations');
$this->db->select(" CONCAT_WS(',', clb_company.address1, clb_company.address2, clb_company.city, clb_company.state, clb_company.zipcode ) as companyAddress");
$this->db->from($this->_table);
$this->db->join($this->_table_device, $fieldname1. " = ".  $fieldname2, 'LEFT');
$this->db->where($blablafield , '0');
----------------

错误如下:

代码语言:javascript
复制
Error Number: 1064

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 
'FROM (`clb_device`) JOIN `clb_company` ON `clb_company`.`id` = `clb_device`.`com' at line 2

SELECT `clb_device`.`id` as deviceId, `clb_pricing_specifications`.`name` as pricingSpecName, `clb_company`.`name` as companyName, `clb_device`.`mac_address` as deviceMacAddress, 
`clb_device`.`reseller_model_number` as deviceModelNumber, `clb_pricing_spec_grouping`.`pricing_master_spec_id` as pricingSpecId, `clb_device`.`address` as deviceAddress, 
`clb_device`.`is_home` as deviceIsHomeCharger, CONCAT(clb_company.portal_line1, `'/'`, `clb_device`.`name)` as deviceDisplayName FROM (`clb_device`) JOIN `clb_company` 
ON `clb_company`.`id` = `clb_device`.`company_id` LEFT JOIN `clb_pricing_group_devices` ON `clb_device`.`id` = `clb_pricing_group_devices`.`device_id` and clb_pricing_group_devices.is_active = 1 
LEFT JOIN `clb_pricing_spec_grouping` ON `clb_pricing_group_devices`.`pricing_spec_id` = `clb_pricing_spec_grouping`.`pricing_master_spec_id` LEFT JOIN `clb_pricing_specifications` ON 
`clb_pricing_spec_grouping`.`pricing_spec_id` = `clb_pricing_specifications`.`id` WHERE clb_company.vendor_id is not null AND cast(substr(clb_devi
ce.software_version, 1, 3) as decimal(2,1)) > 2.0 AND clb_device.device_state > 0 GROUP BY `clb_device`.`id` ORDER BY CONCAT(trim(clb_company.portal_line1), `'/'`, trim(clb_device.name)) desc LIMIT 20

看看CONCAT(trim(clb_company.portal_line1),'/',trim(clb_device.name)

请提出解决办法。

EN

回答 8

Stack Overflow用户

发布于 2013-01-09 09:48:14

在查询之前使用这一行:

代码语言:javascript
复制
$this->db->_protect_identifiers=false;

这将停止向生成的查询添加回退。

票数 32
EN

Stack Overflow用户

发布于 2012-06-07 22:04:59

解决方案非常简单:在数据库配置文件(./application/config/database.php)中添加一个具有默认设置的数组新元素。

代码语言:javascript
复制
$db['default']['_protect_identifiers']= FALSE;

这个解决方案是为我工作,更优雅和专业。

票数 15
EN

Stack Overflow用户

发布于 2013-09-11 19:00:32

所有其他答案都是老掉牙的,这一条适用于CI 2.1.4

代码语言:javascript
复制
// set this to false so that _protect_identifiers skips escaping:
$this->db->_protect_identifiers = FALSE;

// your order_by line:
$this -> db -> order_by('FIELD ( products.country_id, 2, 0, 1 )');

// important to set this back to TRUE or ALL of your queries from now on will be non-escaped:
$this->db->_protect_identifiers = TRUE;
票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7482594

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档