Search code examples
phpmysqlsearch-form

How would I go about using a string value, such as '100pF 50vdc' and query multiple different fields as part of a search?


This is the current query that is working

$query_CDC="SELECT * FROM ceramic_capacitors WHERE voltage LIKE '%$search%' OR vType LIKE '%$search%' OR CONCAT(voltage, vtype) LIKE '%$search%' OR value LIKE '%$search%' OR mult LIKE '%$search%' OR CONCAT(value, mult) LIKE '%$search%' OR source LIKE '%$search'";

I am trying to search the entire table, with spaces in the search field. I added the CONCAT to the query, and it will only return entries when there are no spaces in the search field


Solution

  • Use a regular expression to split the search string.

    if (preg_match('/^(\d+)([a-z]+)$/i', $search, $match)) {
        $number = $match[1];
        $mult = $match[2];
        // Then use $number and $mult in your query
    }