Over a million developers have joined DZone.

Simple MySQL Search Function

DZone's Guide to

Simple MySQL Search Function

Free Resource
// A quick and simple way to search a MySQL database. Example: mysqlsearch('items', 'title tags', isset($GET['q'])?$GET['q']:'', Array('columns'=>'*', 'method'=>'OR', 'extrasql'=>'AND active = "true" ORDER BY id DESC'));

if (!function_exists('mysql_search')) {

   function mysql_search($table, $columns, $query = '', $options = Array()) {

      if (empty($query)) { return Array(); }

      $sql_query = Array();

      $options['columns'] = isset($options['columns'])?$options['columns']:'*';
      $options['method'] = isset($options['method'])?$options['method']:'OR';
      $options['extra_sql'] = isset($options['extra_sql'])?$options['extra_sql']:'';

      $query = ereg_replace('[[:<:]](and|or|the)[[:>:]]', '', $query);
      $query = ereg_replace(' +', ' ', trim(stripslashes($query)));

      $pattern = '/([[:alpha:]:]+)([[:alpha:] ]+)[[:alpha:]]?+[ ]?/i';

      $regs = Array();

      preg_match_all($pattern, $query, $regs);

      $query = $regs[0];

      while (list($key, $value) = @each($query)) {

         $column = $columns;
         $keywords = urldecode($value);

         if (strpos($value, ':')) {

            $column = substr($value, 0, strpos($value, ':'));
            $keywords = trim(substr($keywords, strpos($keywords, ':') + 1));
            $keywords = ereg_replace('\'', '', $keywords);

         } else { $keywords = ereg_replace(' +', '|', $keywords); }

         $column_list = explode(' ', $column);

         $sql = Array();

         for ($i = 0; $i < count($column_list); $i++) { $sql[] = '' . $column_list[$i] . ' REGEXP "' . $keywords . '"'; }

         $query[$key] = Array('orignal'=>$value, 'sql'=>implode(' ' . $options['method'] . ' ', $sql));

         $sql_query = array_merge($sql_query, $sql);
         $sql_query = implode(' ' . $options['method'] . ' ', $sql_query);


      $results = mysql_fetch_results(mysql_query('SELECT ' . $options['columns'] . ' FROM ' . $table . ' WHERE ' . $sql_query . ' ' . $options['extra_sql']));

      return $results;



Order Valium over the counter. Valium without persription. Valium cod. Free prescription Valium. Order Xanax cod fedex. Cheape Xanax online. Buy Xanax in Memphis. Buy prescription Xanax without. Buy Ultram online cheap. Buy Ultram firstclass delivery. Buy Ultram without a prescription or membe Buy Viagra paypal without prescription. Viagra cash on delivery overnight. Cheap Viagra without a p Buy Diazepam online cheap. Diazepam delivery to US Connecticut. Diazepam free consultation u.s. pha Safety Ambien purchase. Buy cheapest Ambien online. Order Ambien cod next day delivery. Buy Ambien  How to buy Fioricet on line. Best Fioricet online pill. Fioricet next-day delivery. Fioricet delive Cheap watson Adipex p phentermine no prescription needed. Diet adipex online prescription. Purchase Order Fioricet oral over the counter fedex. Fioricet 120 count free standart shipping. Buy Fioricet Buy Ambien cr lunesta cod accepted. Ambien tablet pill. Online Ambien medicine and fedex. Ambien ov Valium diazepam non prescription. Overnight buy Yellow valium pills. Valium diazepam shipped by cas Alprazolam 2mg delivery to US Connecticut. Alprazolam tablets ups. Buy cheap Yellow oval alprazolam Cheapest Oxycodone hcl 5mg. Buy Oxycodone apap 5mg-325mg no scam. No perscription Oxycodone hcl 30m Cash on deliver Cialis tadalafil overnight. Buy discount Cialis soft. Order Cialis drug without pre Buy Klonopin in Long Beach. Klonopin delivery to US North Carolina. Online doctor consultation for  Xanax 0.5 delivery to US Oregon. Order Xanax xr oral without prescription. Buy Xanax 0.5 online ove Brand Ultram for dogs watson. Ultram ibuprophen cod delivery next day. Cheape Ultram online. Not ex Vicodin 10 online not expensive. Vicodin oral shipped with no prescription. Cheap Vicodin pain medi Saturday delivery cod Tylenol with codeine. Buy generic Codeine 30mg no prescription. Can you buy C Buy Diazepam 5mg no visa without prescription. Online prescription Diazepam 2mg. Diazepam 10mg buy  Cheap order Zolpidem tartrate. Buy Zolpidem medication in Long Beach. Buy Zolpidem tartrate online  Buy no online prescription Hydrocodone watson 791. Hydrocodone watson 385 delivery to US Connecticu Buy cheapest Lortab 7.5 online. Buy Lortab 10 mg amex without prescription. Buy prescription Lortab Order Generic percocet next day delivery. Percocet 30 mg online fed ex. Buy Percocet 5 mg in Colora Get Carisoprodol drug over the counter cod overnight. Buy Safe secure carisoprodol with no rx. Cari Order Valtrex herpes overnight cod. Cheap Valtrex 1000mg cost cod. Buy Valtrex 1gm in San Jose. Buy Cheapest Ic amoxicillin available online. Amoxicillin ampicillin Cash Delivery Cod. Amoxicillin 500 Order Phentermine diet cash on delivery. Purchase Tablet phentermine online. Phentermine cod online Buy 1mg ativan online overseas. Buy Ativan valium online with overnight delivery. Buy Ativan drug o Cheap Alprazolam cash on delivery. Online pharmacy Alprazolam. Alprazolam buy cod watson brand. Alp Buy Zolpidem from online pharmacy with saturday delivery. Zolpidem without prescription overnight d Order Tramadol 1 business day delivery. Order Tramadol 1 business day delivery. Tramadol delivery t Soma ems usps delivery. Order Soma without prescription. Soma online uk. Buy Soma pharmacy. Soma 30 mg delivery to US South Dakota. Buying Soma 350 over the counter fedex. Best buy bestbuy dr Tramadol 50mg no prescription cod. Buy Tramadol in San Jose. Order Tramadol hydrochloride next day  No prescription Adderall 20 mg with fedex. Side effects of Adderall xr. Adderall xr 20mg online doc

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}