ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Query Module advanced Queries give errors

November 03, 2010 7:20am

Subscribe [4]
  • #1 / Nov 03, 2010 7:20am

    H&O

    34 posts

    I have the following query:

    SELECT @field_id:=CONCAT('d.field_id_',field_id) FROM exp_category_fields WHERE group_id = '2' AND field_name = 'title_nl';
    
    SET @query = CONCAT('SELECT t.cat_id AS child_category_id, t.cat_name AS child_category_name, ',@field_id,' AS title FROM exp_categories AS t JOIN exp_category_field_data AS d USING(cat_id) WHERE t.parent_id != 0 AND t.group_id = 2 AND ',@field_id,' != \'\' ORDER BY ',@field_id,' ASC;');
    
    PREPARE stmt FROM @query;
    EXECUTE stmt;

    This query exectutes as expected, but when i try to run it i get the following error:

    MySQL ERROR:
    
    Error Number: 1064
    
    Description: 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 'SET @query = CONCAT('SELECT t.cat_id AS child_category_id, t.cat_name AS child_c' at line 1
    
    Query: SELECT @field_id:=CONCAT('d.field_id_',field_id) FROM exp_category_fields WHERE group_id = '2' AND field_name = 'title_nl'; SET @query = CONCAT('SELECT t.cat_id AS child_category_id, t.cat_name AS child_category_name, ',@field_id,' AS title FROM exp_categories AS t JOIN exp_category_field_data AS d USING(cat_id) WHERE t.parent_id != 0 AND t.group_id = 2 AND ',@field_id,' != \'\' ORDER BY ',@field_id,' ASC;'); PREPARE stmt FROM @query; EXECUTE stmt;

    Any ideas?

  • #2 / Nov 03, 2010 4:30pm

    Lisa Wess

    20502 posts

    Hi, H&O - how are you executing this query? Are you attempting to us the query module?

  • #3 / Nov 04, 2010 6:42am

    H&O

    34 posts

    I’m using the following code in my template.

    {exp:query sql="
    SELECT @field_id:=CONCAT('d.field_id_',field_id) FROM exp_category_fields WHERE group_id = '2' AND field_name = 'title_nl';
    
    SET @query = CONCAT('SELECT t.cat_id AS child_category_id, t.cat_name AS child_category_name, ',@field_id,' AS title FROM exp_categories AS t JOIN exp_category_field_data AS d USING(cat_id) WHERE t.parent_id != 0 AND t.group_id = 2 AND ',@field_id,' != \\'\\' ORDER BY ',@field_id,' ASC;');
    
    PREPARE stmt FROM @query;
    EXECUTE stmt;"}
    <option value="{title}">{title}</option>
    {/exp:query}

    This gives the above error if I run it, but if I run the same query with my MySQL (Sequel Pro) program there are no problems.

  • #4 / Nov 04, 2010 3:37pm

    Ingmar

    29245 posts

    The query module is meant to only run SELECT statements. If you need anything more advanced than that it’s probably best to use raw PHP, as it were.

  • #5 / Nov 04, 2010 4:10pm

    H&O

    34 posts

    Hi,

    I understand. I wanted to put the sql query in a Solspace Template Morsel, morsels do not support PHP, I guess i’ll have to create it into a small plugin, no problem.

  • #6 / Nov 05, 2010 5:05am

    John Henry Donovan

    12339 posts

    No problems, thanks for following up. Feel free to start a new thread if you have any more questions.

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases