DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Simple MySQL Search Function

03.21.2011
| 4768 views |
  • submit to reddit
        // 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;

   }

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