Note that there are some explanatory texts on larger screens.

plurals
  1. POphp multiple word search algorithm/script doesn't work with single quotes around search array variable. what am i doing wrong?
    primarykey
    data
    text
    <p>i've got a basic search code to search through database when people search for more than 1 word. i've done loads of research and all the algortihms seem to be prety much the same. trim the search words. <code>explode</code> them into an <code>array</code>. then use either <code>foreach</code> or <code>while</code> to add each word of array onto the <code>msql_query</code>.</p> <p>but there is a problem. here is my code:</p> <pre><code>if (isset($_POST['search'])){ $words = $_POST['searchfield']; $arraySearch = explode(" ", trim($words)); $countSearch = count($arraySearch); $a = 0; $query = "SELECT * FROM parts WHERE "; while ($a &lt; $countSearch) { $query = $query."description RLIKE '$arraySearch[$a]'"; $a++; if ($a &lt; $countSearch) { $query = $query." AND "; } } $results=mysql_query($query) or die($query); if(!$results){ $msg = "No results, please try another search";} } </code></pre> <p>ok so look at the second <code>$query</code> variable where it says <code>"decription RLIKE '$arraySearch'"</code></p> <p>For the search to work whatever word is in <code>$arraySearch</code> HAS to be in single quotes. BUT when i try it the script will just not run. BUT if i take away the single quotes the script runs. but it doesn't perform the search. it <code>die</code>s and comes up with error. in this case i've made the error message the actual query to try and find out what's going wrong.</p> <p>so if i take away the single quotes and search for "car tyre". the query will be <code>SELECT * FROM parts WHERE description RLIKE car AND description RLIKE tyre</code> but it won't work unless its like this: <code>SELECT * FROM parts WHERE description RLIKE 'car' AND description RLIKE 'tyre'</code>.</p> <p>i know this because i have tested the earch by just typing it into another query to test it.</p> <p>i've spent hours and hours trying to figure a way round this but i can't figure it out. why is it doing this to me? how do i get round it? and why doesn't anyone else seem to be having the same problem?????</p> <p>thanx for help :)</p> <p>.... added as afterthough: i'm thinking that the only possible way of doing this is to have the single quotes already inside the variable. but i don't know how to do that. as in: <code>$arraySearch = ("'car'", "'tyre'");</code> any ideas?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload