Working on a port of imagepicker module from D6 to D7, Debian 5 standard lamp stack, PHP 5.2.0, mysql Ver 14.12 Distrib 5.0.32
I'm having problems getting a db_select statement to work, in particular a db_or() that
will not accept more than one 'LIKE' condition. It will accept multiple '=' conditions
but not 'LIKE'.
Here is the error it throws:
PDOException: SQLSTATE[42000]:
Syntax error or access violation: 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
':db_condition_placeholder_2 ESCAPE '\\') )'
at line 1:
SELECT i.img_id AS img_id, i.uid AS uid, i.img_name AS img_name, i.img_title AS img_title, i.img_description AS img_description, i.img_date AS img_date
FROM {imagepicker} i
WHERE (uid = :db_condition_placeholder_0)
AND( (img_description LIKE :db_condition_placeholder_1 ESCAPE '\\')
OR (img_title LIKE :db_condition_placeholder_2 ESCAPE '\\') );
Array (
[:db_condition_placeholder_0] => 2
[:db_condition_placeholder_1] => %testword%
[:db_condition_placeholder_2] => %testword%
)
in _imagepicker_browse()
(line 965 of /mnt/sdb1/www/drupal-7.x-dev/sites/all/modules/imagepicker/imagepicker.module).
If I rewrite the above SQL to work in phpmyadmin:
SELECT i.img_id AS img_id, i.uid AS uid, i.img_name AS img_name, i.img_title AS img_title, i.img_description AS img_description, i.img_date AS img_date
FROM imagepicker i
WHERE (uid = 2)
AND( (img_description LIKE '%testword%')
OR (img_title LIKE '%testword%') )
It works fine.
I have tried various combinations of 'LIKE' and '=' with and without wildcards
and the result is the same, only one 'LIKE' condition allowed.
Here is a snip of the code:
return db_or()
->condition('img_description', '%testword%', 'LIKE')
->condition('img_title', '%testword%', 'LIKE');
This one fails
If I hardwire '='
return db_or()
->condition('img_description', '%testword%', 'LIKE')
->condition('img_title', 'testword', '=');
It works
I have tried 3 conditions, changed the order but nothing will let more than
one 'LIKE' condition through
Looks like a bug but it could easily be me.
Comments
Comment #1
Dave ReidOdd. I don't have this same problem. In my path_redirect D7 port, I do the following:
I added
dpm((string) $query);
to the end of the function and the resulting query was:No error on the filtered page with this query, so I'm not sure why it's not working for you. :/
Comment #2
hutch CreditAttribution: hutch commentedThanks for your interesting posting, it still won't work but I'm closer I think.
Comment #4
mr.baileysWorks fine on my end too:
Mind if we switch this to a support request unless it's proven to be a bug?
Comment #7
flefle CreditAttribution: flefle commentedThis issue will be automatically closed, feel free to reopen it if needed. mr.baileys's solution seems to be reasonable.
Comment #9
fabul CreditAttribution: fabul commentedHi,
For information : I had an error 42000 when I used a db_select statement without fields statement using the 'users' table.
This is my example :
Am I wrong when I fon't use fields statement as I'd like to read all the fields of the records ?
cheers.