MySQL データベースを検索するための迅速かつ簡単な方法。 例: 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)) { 配列を返します(); }
-
- $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('[[:<:]](および|または|the)[[:>:]]', '', $query);
- $query = ereg_replace(' +', ' ', トリム(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 = トリム(substr($keywords, strpos($keywords, ':') + 1));
- $keywords = ereg_replace(''', '', $keywords);
-
- } else { $keywords = ereg_replace(' +', '|', $keywords); }
-
- $column_list =explode(' ', $column);
-
- $sql = Array();
-
- for ($i = 0; $i
- $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'] . ' WHERE ' . $options['extra_sql']);
-
- }
-
-
-
- 复制代
|