您好,欢迎访问一九零五行业门户网

一个构造sql语句的类

/*** @package database class* @author injection (mail:injection.mail@gmail.com)* @version 1.0*/@ini_set( 'display_errors',0 );class database{ private $mdb_host,$mab_user,$mab_pwd,$mconn_no; function database( $conn_obj ){ $this->connectdb( $conn_obj ); } function connectdb( $conn_obj ){ $this->mdb_host = $conn_obj->host; $this->mad_name = $conn_obj->user; $this->mad_pwd = $conn_obj->pwd; $this->mconn_no = mysql_connect( $this->mdb_host, $this->mad_name, $this->mad_pwd ); } function selectdb( $conn_obj ){ $this->mdb_name = $conn_obj->dbname; mysql_select_db( $this->mdb_name ); }}/*** @package making sqls class exetends database class* @author injection (mail:injection.mail@gmail.com)* @version 1.0*/class makesql extends database{ private $msql; function makesql( $type,$arr_colum_list, $arr_sql_choice ){ $this->makesqltype( $arr_colum_list, $arr_sql_choice ); } #switch make list function makesqltype( $type, $arr_colum_list, $arr_sql_choice ){ switch( $type ){ case 'insert': return $this->makeinsert( $arr_colum_list, $arr_sql_choice ); case 'select': return $this->makeselect( $arr_colum_list, $arr_sql_choice ); case 'update': return $this->makeupdate( $arr_colum_list, $arr_sql_choice ); case 'delete': return $this->makedelete( $arr_colum_list, $arr_sql_choice ); } } #make insert function makeinsert( $arr_colum_list,$arr_sql_choice ){ $colum_key = array_keys( $arr_colum_list ); $colum_value = array_values( $arr_colum_list ); $this->msql = insert into .$arr_sql_choice[tbl_name].( .join( ',' , $colum_key ). ) values( '.join( ',' , $colum_value ).'); return $this->msql; } #making select function makeselect( $arr_colum_list = '*' , $arr_sql_choice ){ $colum_value = array_keys( $arr_colum_list ); foreach( $arr_sql_choice as $sql_key => $sql_value ){ if( strcmp( $sql_key, 'tbl_name' ) == 0 ){ if( strcmp($arr_colum_list, '*' ) !== 0 ) $this->msql = select .join( ',' , $colum_value ). from .$sql_value; else $this->msql = select * from .$sql_value; } else if( strcmp( $sql_value, '' ) !== 0 ) if(strcmp( $sql_key, 'where' ) === 0 && strcmp( $sql_value, 'colum' ) === 0 ){ foreach($arr_colum_list as $colum_key => $colum_value ) $this->msql .= $colum_key = '$colum_value' and ; $this->msql = rtrim( $this->msql, and ); } else $this->msql .= $sql_key .$sql_value; } return $this->msql; } #making update function makeupdate( $arr_colum_list, $arr_sql_choice ){ $this->msql = update .$arr_sql_choice['tbl_name']. set ; foreach( $arr_colum_list as $colum_key => $colum_value ) $this->msql .= $colum_key = '$colum_value',; $this->msql = rtrim( $this->msql , ','); foreach( $arr_sql_choice as $sql_key => $sql_value ){ if( strcmp( $sql_value, '' ) !== 0 && strcmp( $sql_key, 'tbl_name' ) !==0 && strcmp( $sql_key, 'order by' ) !== 0 ) $this->msql .= $sql_key .$sql_value; } return $this->msql; } #making delete function makedelete( $arr_colum_list, $arr_sql_choice ){ $this->msql = delete from .$arr_sql_choice['tbl_name']; foreach( $arr_sql_choice as $sql_key => $sql_value ){ if( strcmp( $sql_key, 'tbl_name' ) !== 0 && strcmp( $sql_value, '' ) !== 0 ){ $this->msql .= $sql_key .$sql_value; } } return $this->msql; }}
复制代码
其它类似信息

推荐信息