Welcome Guest, Not a member yet? Register   Sign In
Db prefix + dots in the query problem
#1

[eluser]GIN[/eluser]
Hi.

I'm not sure is that bug or not, but here is another similar problem http://ellislab.com/forums/viewthread/77767/
Anyway, my problem is:

I have a db prefix - sg_
I have this active record code:
Code:
$this->db->select('g.id, g.name, g.eng_name, g.price, g.code,
IF(CHAR_LENGTH(g.name)>40, CONCAT(LEFT(g.name, 40), "..."),g.name) as short_name,
gi.filename as preview, CONCAT(g.eng_name,"-",g.id) as nameid',FALSE)
->from('goods as g')
->join('goods_img as gi','g.id = gi.good_id AND gi.type = 0','left')
->where('g.id IN ('.join(',',$good_ids).') AND g.warehouse = 1 AND g.active = 1')
->groupby('g.id')
->orderby('g.priority');
$result = $this->db->get()->result();
And so what I got after $this->db->last_query():
Code:
SELECT g.id, g.name, g.eng_name, g.price, g.opt_price, g.code,
IF(CHAR_LENGTH(g.name)>40, CONCAT(LEFT(g.name, 40), "..sg_."), g.name) as short_name,
gi.filename as preview, CONCAT(g.eng_name, "-", g.id) as nameid FROM (sg_goods as g)
LEFT JOIN sg_goods_img as gi ON g.id = gi.good_id AND gi.type = 0
WHERE `g`.`id` IN (2855,2856,2857,2858,2859,2860,2861) AND g.warehouse = 1 AND g.active = 1
GROUP BY g.id
ORDER BY g.priority

Look at this section IF(CHAR_LENGTH(g.name)>40, CONCAT(LEFT(g.name, 40), "..sg_."), g.name) as short_name

If I have two or more dots - db_prefix appears in short_name (sg_)
If I have one dot there - I got syntax error in sql query:
IF(CHAR_LENGTH(g.name)>40, CONCAT(LEFT(g.name, 40), sg_"."), g.name) as short_name

Other symbols besides dots not makes problem.
I think problem in _protect_identifiers method in CI_DB_driver class, but can't understand what happens.

What do you think?
#2

[eluser]theprodigy[/eluser]
Instead of:

Quote:$this->db->select('g.id, g.name, g.eng_name, g.price, g.code,
IF(CHAR_LENGTH(g.name)>40, CONCAT(LEFT(g.name, 40), "..."),g.name) as short_name,
gi.filename as preview, CONCAT(g.eng_name,"-",g.id) as nameid',FALSE)

try
Code:
$this->db->select('g.id, g.name, g.eng_name, g.price, g.code,
IF(CHAR_LENGTH(g.name)>40, CONCAT(LEFT(g.name, 40), "…"),g.name) as short_name,
gi.filename as preview, CONCAT(g.eng_name,"-",g.id) as nameid',FALSE)

EDIT: Change "..." into "& hellip;"(minus the space between & and hellip)
#3

[eluser]GIN[/eluser]
Yes, I understand, but is that normal that I can't use one, two or more dots in sql query?
#4

[eluser]theprodigy[/eluser]
starting on line 1256 of the CI_DB_driver class (in the _protect_identifiers function)
Quote:// Break the string apart if it contains periods, then insert the table prefix
// in the correct location
, assuming the period doesn't indicate that we're dealing
// with an alias. While we're at it, we will escape the components
Then on line 1261:
Code:
$parts    = explode('.', $item);
It is finding your periods and exploding on them so that it can append the table prefix that you have assigned.
#5

[eluser]GIN[/eluser]
Ok. I see.
Thanks.




Theme © iAndrew 2016 - Forum software by © MyBB