Note that there are some explanatory texts on larger screens.

plurals
  1. POMulti word search in PHP/MySQL
    text
    copied!<p>I'm struggling to create a search that searches for multiple words. My first attempt yielded no results whatsoever and is as follows:</p> <pre><code> require_once('database_conn.php'); if($_POST){ $explodedSearch = explode (" ", $_POST['quickSearch']); foreach($explodedSearch as $search){ $query = "SELECT * FROM jobseeker WHERE forename like '%$search%' or surname like '%$search%' ORDER BY userID LIMIT 5"; $result = mysql_query($query); } while($userData=mysql_fetch_array($result)){ $forename=$userData['forename']; $surname=$userData['surname']; $profPic=$userData['profilePicture']; $location=$userData['location']; echo "&lt;div class=\"result\"&gt; &lt;img class=\"quickImage\" src=\"" . $profPic. "\" width=\"45\" height=\"45\"/&gt; &lt;p class=\"quickName\"&gt;" . $forename . " " . $surname . "&lt;/p&gt; &lt;p class=\"quickLocation\"&gt; " . $location . "&lt;/p&gt; &lt;/div&gt;"; } } </code></pre> <p>I also tried the following, which yielded results, but as you can imagine, I was getting duplicate results for every word I entered:</p> <pre><code>if($_POST){ $explodedSearch = explode (" ", $_POST['quickSearch']); foreach($explodedSearch as $search){ $query = "SELECT * FROM jobseeker WHERE forename like '%$search%' or surname like '%$search%' ORDER BY userID LIMIT 5"; $result .= mysql_query($query); while($userData=mysql_fetch_array($result)){ $forename=$userData['forename']; $surname=$userData['surname']; $profPic=$userData['profilePicture']; $location=$userData['location']; echo "&lt;div class=\"result\"&gt; &lt;img class=\"quickImage\" src=\"" . $profPic. "\" width=\"45\" height=\"45\"/&gt; &lt;p class=\"quickName\"&gt;" . $forename . " " . $surname . "&lt;/p&gt; &lt;p class=\"quickLocation\"&gt; " . $location . "&lt;/p&gt; &lt;/div&gt;"; } } } </code></pre> <p>I'm pretty much at a loss as to how to proceed with this, any help would be greatly appreciated.</p> <p>EDIT:</p> <pre><code>if($_POST){ $quickSearch = $_POST['quickSearch']; $explodedSearch = explode (" ", trim($quickSearch)); $queryArray = array(); foreach($explodedSearch as $search){ $term = mysql_real_escape_string($search); $queryArray[] = "forename like '%" . $term . "%' surname like '%" . $term . "%'"; } $implodedSearch = implode(' or ', $queryArray); $query="SELECT * FROM jobseeker WHERE ($implodedSearch) ORDER BY userID LIMIT 5"; $result = mysql_query($query); while($userData=mysql_fetch_array($result, MYSQL_ASSOC)){ $forename=$userData['forename']; $surname=$userData['surname']; $profPic=$userData['profilePicture']; $location=$userData['location']; echo "&lt;div class=\"result\"&gt; &lt;img class=\"quickImage\" src=\"" . $profPic. "\" width=\"45\" height=\"45\"/&gt; &lt;p class=\"quickName\"&gt;" . $forename . " " . $surname . "&lt;/p&gt; &lt;p class=\"quickLocation\"&gt; " . $location . "&lt;/p&gt; &lt;/div&gt;"; } } </code></pre>
 

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