PHP实现的构造sql语句类实例
程序员文章站
2024-04-03 17:26:16
本文实例讲述了php实现的构造sql语句类。分享给大家供大家参考,具体如下:
/**
* @package database class
* @author...
本文实例讲述了php实现的构造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; } }
更多关于php相关内容感兴趣的读者可查看本站专题:《php+mysql数据库操作入门教程》、《php防止sql注入方法总结》、《php基于pdo操作数据库技巧总结》及《php常见数据库操作技巧汇总》
希望本文所述对大家php程序设计有所帮助。