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 8500233
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T00:53:25+00:00 2026-06-11T00:53:25+00:00

I am trying to get results from a MySQL database by searching multiple terms

  • 0

I am trying to get results from a MySQL database by searching multiple terms in multiple fields. The script below is working, but not quite the way I need it and I am having trouble finding out a way of doint it.

Currently if I search for “apples peaches” it will return every result that has “apples” OR “peaches” on any field. I.e. if a record has “apples” but not “peaches” it will yield a result.

With the intended behavior I should get a result only if in a given record it finds all the search terms on any field. I.e. “apples” could be found on field f1 and “peaches” on field f2. If only “apples” is found then it is not a result.

<?php
if (!isset($_REQUEST['term'])) exit;
$dblink = mysql_connect('localhost', 'root', 'pass') or die(mysql_error());
mysql_select_db('mytable');
mysql_set_charset('utf8', $dblink);
// query the database table for strings that match 'term'
$lcSearchVal = mysql_real_escape_string($_REQUEST['term']);
$lcSearchVal = explode(' ', $lcSearchVal);
$sql = 'SELECT id, f1, f2, f3, f4 FROM mytable 
        WHERE (';
$parts = array();
foreach($lcSearchVal as $lcSearchWord) {
    if ($lcSearchWord <> "") {
        $parts[] = '`id` LIKE "%' . $lcSearchWord . '%"';
    }
    if ($lcSearchWord <> "") {
        $parts[] = '`f1` LIKE "%' . $lcSearchWord . '%"';
    }
    if ($lcSearchWord <> "") {
        $parts[] = '`f2` LIKE "%' . $lcSearchWord . '%"';
    }
    if ($lcSearchWord <> "") {
        $parts[] = '`f3` LIKE "%' . $lcSearchWord . '%"';
    }
    if ($lcSearchWord <> "") {
        $parts[] = '`f4` LIKE "%' . $lcSearchWord . '%"';
    }
}
$sql.= implode(' OR ', $parts) . ') order BY id desc limit 0,10';
$rs = mysql_query($sql, $dblink);
// loop through each string returned and format the response for jQuery
$data = array();
$data[] = array('value' => "", 'label' => " >> Last 10 results:", 'desc' => "");
if ($rs && mysql_num_rows($rs)) {
    while ($row = mysql_fetch_array($rs, MYSQL_ASSOC)) {
        if (strlen($row['f3']) > 250) {
            $row['f3'] = substr($row['f3'], 0, 250) . " [...]";
        }
        $data[] = array('value' => $row['id'], 'label' => $row['id'] . '/' . $row['f1'] . ' (' . $row['f2'] . ') ' . $row['f4'], 'desc' => $row['f3']);
    }
}
// jQuery wants JSON data
echo json_encode($data);
flush();
?>

Thank you for your input

  • 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-06-11T00:53:26+00:00Added an answer on June 11, 2026 at 12:53 am

    Consider the following:

    $lcSearchVal = 'one two three';
    $lcSearchVal = explode(' ', $str);
    foreach ($lcSearchVal as &$lcSearchWord) {
        $lcSearchWord = '`id` LIKE "%' . $lcSearchWord . '%" AND ';
    }
    $conditions = rtrim(implode('', $val), ' AND ');
    
    echo $conditions; //returns `id` LIKE "%one%" AND `id` LIKE "%two%" AND `id` LIKE "%three%"
    

    $conditions can then be inserted into the query as the WHERE condition.

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

Sidebar

Related Questions

I'm trying to get results from a MySQL database (for invoices) that includes multiple
Im trying to get results from a mysql database to properly format into columns,
I am trying to get the correct formatting of results back from a Mysql
Im trying to get data from a mysql database and display it in an
I am trying to get 3 results out of a mysql database at a
I am trying veru hard, to get results from a database to be displayed
I am using Entity Framework. I am trying to get the results from linq
I trying to search a MySQL database using PHP, The search works fine but
I'm trying to query my database to get some results in Chinese and Japanese
Trying to use Zend_Search_Lucene to search a MySql database and display the results. Running

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.