Support » Fixing WordPress » Error in SQL syntax

  • I have installed the “Page Category Organiser” plugin which among others creates the table page2cat.

    Why I try the following query, I get an error message:
    <?php
    $subpages = $wpdb->get_results("
    SELECT post_title
    FROM $wpdb->posts p
    INNER JOIN $wpdb->page2cat p2c
    ON p2c.page_id = p.ID
    WHERE p2c.pagecat_id = 2
    ");
    ?>

    The error message is:

    WordPress database error: [Table ‘chrismochta_nl.p2c’ doesn’t exist]
    SELECT post_title FROM wp_posts p INNER JOIN p2c ON p2c.page_id = p.ID WHERE p2c.pagecat_id = 2

    When I change the SQL query, I still get an error message (I only changed the aliasses p en p2c)
    <?php
    $subpages = $wpdb->get_results("
    SELECT post_title
    FROM $wpdb->posts
    INNER JOIN $wpdb->page2cat
    ON $wpdb->page2cat.page_id = p.ID
    WHERE $wpdb->page2cat.pagecat_id = 2");
    ?>

    The error message is now:

    WordPress database error: [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 ‘ON .page_id = p.ID WHERE .pagecat_id = 2’ at line 4]
    SELECT post_title FROM wp_posts INNER JOIN ON .page_id = p.ID WHERE .pagecat_id = 2

    Can anyone tell what is wrong with this query?

Viewing 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Error in SQL syntax’ is closed to new replies.