php 简单mysql数据库查询函数
内容摘要
这篇文章主要为大家详细介绍了php 简单mysql数据库查询函数,具有一定的参考价值,可以用来参考一下。
php简单的mysql查询函数,对此感兴趣的朋友,看看idc笔记做的技术笔记。经测
php简单的mysql查询函数,对此感兴趣的朋友,看看idc笔记做的技术笔记。经测
文章正文
这篇文章主要为大家详细介绍了php 简单mysql数据库查询函数,具有一定的参考价值,可以用来参考一下。
php简单的mysql查询函数,对此感兴趣的朋友,看看idc笔记做的技术笔记。经测试代码如下:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | <code class = "php" > /** * 简单的mysql查询函数 * * @param * @arrange (www.idcnote.com) **/ 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 ; } } /*** 来自php教程(www.idcnote.com) ***/ </code> |
注:关于php 简单mysql数据库查询函数的内容就先介绍到这里,更多相关文章的可以留意
代码注释