Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7026681
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:08:59+00:00 2026-05-28T00:08:59+00:00

I’m currently trying to update my in house search engine to use multiple words.

  • 0

I’m currently trying to update my in house search engine to use multiple words. It’s very big and complicated but I’m running into errors I never ran into before now and I have no idea why.

so the question is, why am I receiving the error below?

I’m going to split this up into different sections for better understanding.

This is just the SQL query that I echod, copy and pasted into PHPMyAdmin, pasted here (PHPMyAdmin formatted it nicely) It searches for 2 words in the database:

SELECT *
FROM (

SELECT p.page_url AS url, COUNT( * ) AS occurrences
FROM PAGE p, word w, occurrence o
WHERE (
(
p.page_id = o.page_id
AND w.page_word_id = o.page_word_id
AND w.word_word LIKE '%' 'test' '%'
GROUP BY p.page_id
)
OR (
p.page_id = o.page_id
AND w.page_word_id = o.page_word_id
AND w.word_word LIKE '%' 'search' '%'
GROUP BY p.page_id
)
UNION (

SELECT f.file_url AS url, COUNT( * ) AS occurrences
FROM files f, filenames fn, fileoccurrence fo
WHERE f.file_id = fo.file_id
AND fn.file_word_id = fo.file_word_id
AND fn.file_word LIKE '%' 'test' '%'
GROUP BY f.file_id
)
OR (

SELECT f.file_url AS url, COUNT( * ) AS occurrences
FROM files f, filenames fn, fileoccurrence fo
WHERE f.file_id = fo.file_id
AND fn.file_word_id = fo.file_word_id
AND fn.file_word LIKE '%' 'search' '%'
GROUP BY f.file_id
)
)t
ORDER BY occurrences DESC 

This code is produced by this PHP code below which uses the explode function from the search input

// Do a little formatting
$keyword = strtolower($keyword);

// Get timestamp for start
$start_time = microtime(true);

$searched_words = explode(' ', $keyword);

foreach ($searched_words as $index => $word) {
    // Set up the stemmer
    $stemmer = new PorterStemmer;
    $stemmed_string = $stemmer->stem($word);
    $searched_words[$index] = $stemmed_string;
}

//  Configure the sql code
$sql = "SELECT * FROM (SELECT p.page_url AS url, COUNT(*) AS occurrences 
    FROM page p, word w, occurrence o WHERE (";

// Add the extra words to the sql
foreach ($searched_words as $index => $word) {  
    $sql .= "(p.page_id = o.page_id AND w.page_word_id = o.page_word_id
        AND w.word_word LIKE '%' '" . $word . "' '%' GROUP BY p.page_id) OR";
}
// Add the union to the sql and then add the second query   
$sql = substr($sql, 0, (strLen($sql)-3)); //this will eat the last OR
$sql .= " UNION ";

// The second set of querys
foreach ($searched_words as $index => $word) {
    $sql .= "(SELECT f.file_url AS url, COUNT(*) AS occurrences FROM files f, filenames fn, fileoccurrence fo
        WHERE f.file_id = fo.file_id AND fn.file_word_id = fo.file_word_id AND fn.file_word
        LIKE '%' '" . $word . "' '%' GROUP BY f.file_id) OR";
}

// Clsoe the sql code
$sql = substr($sql, 0, (strLen($sql)-3)); //this will eat the last OR
$sql .= ") t ORDER BY occurrences DESC"; // LIMIT " . $results . "");

// echo the query for the pure lolz of it
echo $sql . "<br /><br />";

// Search the DB for the results
$results = mysql_query($sql)
    or die("Invalid query: " . mysql_error());

All of this returns the error:

Invalid query: 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 ‘GROUP BY p.page_id) OR(p.page_id = o.page_id AND
w.page_word_id = o.page_word_id’ at line 3

Why am I receiving this error? The code I used previously worked fine. The only real thing I added was the foreach().

This is my original SQL code:

$result = mysql_query("SELECT * FROM (SELECT p.page_url AS url, COUNT(*) AS occurrences 
    FROM page p, word w, occurrence o WHERE p.page_id = o.page_id AND w.page_word_id = o.page_word_id
    AND w.word_word LIKE '%' '" . $stemmed_string . "' '%' GROUP BY p.page_id UNION  
    SELECT f.file_url AS url, COUNT(*) AS occurrences FROM files f, filenames fn, fileoccurrence fo
    WHERE f.file_id = fo.file_id AND fn.file_word_id = fo.file_word_id AND fn.file_word
    LIKE '%' '" . $stemmed_string . "' '%' GROUP BY f.file_id) t ORDER BY occurrences DESC") // LIMIT " . $results . "")
        or die("Invalid query: " . mysql_error());

EDIT: Fixed the above error. with this code

//  Configure the sql code
$sql = "SELECT * FROM (SELECT p.page_url AS url, COUNT(*) AS occurrences 
    FROM page p, word w, occurrence o WHERE (";

// Add the extra words to the sql
foreach ($searched_words as $index => $word) {  
    $sql .= "(p.page_id = o.page_id AND w.page_word_id = o.page_word_id
        AND w.word_word LIKE CONCAT('%', '" . $word . "', '%'))) OR "; //GROUP BY p.page_id)
}
// Add the union to the sql and then add the second query   
$sql = substr($sql, 0, (strLen($sql)-4)); //this will eat the last OR
$sql .= " GROUP BY p.page_id)";
$sql .= " UNION ";
$sql .= "(SELECT f.file_url AS url, COUNT(*) AS occurrences FROM files f, filenames fn, fileoccurrence fo
    WHERE (";

// The second set of querys
foreach ($searched_words as $index => $word) {
    $sql .= "(f.file_id = fo.file_id AND fn.file_word_id = fo.file_word_id AND fn.file_word
        LIKE CONCAT('%', '" . $word . "', '%'))) OR "; //GROUP BY f.file_id)
}

// Clsoe the sql code
$sql = substr($sql, 0, (strLen($sql)-4)); //this will eat the last OR
$sql .= " GROUP BY f.file_id)";
$sql .= ") t ORDER BY occurrences DESC"; // LIMIT " . $results . "");

This now produces error:

Invalid query: Every derived table must have its own alias

which has something to do with the unions (i’m not very good at unions.. or SQL in general)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T00:09:00+00:00Added an answer on May 28, 2026 at 12:09 am

    You can’t concatenate strings in SQL just by placing them next to each other.

    AND w.word_word LIKE '%' 'test' '%'
    

    Should be

    AND w.word_word LIKE CONCAT('%', 'test', '%')
    

    Or if you use SET SQL_MODE='PIPES_AS_CONCAT' to get standard ANSI SQL syntax, you can use:

    AND w.word_word LIKE '%' || 'test' || '%'
    

    Re your comment, I see another problem:

    In SQL, the WHERE clause must be complete before you can add a GROUP BY clause. The syntax is:

    WHERE ( <conditions...> )
    GROUP BY <expressions>
    

    Whereas you have:

    WHERE ( <conditions...> GROUP BY <expressions> ) 
       OR ( <conditions...> GROUP BY <expressions> )
    

    What you have is not legal syntax.

    Really, this is the kind of thing you should be able to look up yourself in any beginner reference on SQL.


    Every derived table must have its own alias

    This means that you used a subquery in the FROM clause but did not give it an alias. For example:

    SELECT ... FROM (SELECT ... FROM table) AS x WHERE ...etc... 
    

    If you leave out the AS x then it’s an error (x is just an example in this case, you can choose a more meaningful alias).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I want use html5's new tag to play a wav file (currently only supported
I have a French site that I want to parse, but am running into
I am currently running into a problem where an element is coming back from
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I would like to run a str_replace or preg_replace which looks for certain words

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.