php 简单mysql数据库查询函数

前端之家收集整理的这篇文章主要介绍了php 简单mysql数据库查询函数前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
PHP简单的mySQL查询函数,感兴趣的小伙伴,下面一起跟随编程之家 jb51.cc的小编来看看吧。
经测试代码如下:

/**
 * 简单的mySQL查询函数
 *
 * @param 
 * @arrange (512.笔记) jb51.cc
 **/
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,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;
   }
}
/***   来自编程之家 jb51.cc(jb51.cc)   ***/
原文链接:https://www.f2er.com/php/528885.html

猜你在找的PHP相关文章