一个是类,一个是直接调用。如果说优化的话,个人觉的都不算。在比较大的项目中,主流程文件中,应该避免出现SQL语句。

解决方案 »

  1.   

    应该有个类,类里面包含一些构造sql语句的方法,你只需给这个方法传几个参数,比如表名,字段名等。不应该直接这样写sql语句。用这个函数:
    function makeSelect( $arr_sql_choice , $arr_colum_list = '*'  ){
    $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;
    }如你这条sql,只需传入一个数组,便可构造成功:$arr_sql_choice = array(
    "tbl_name" => "$table_members",
    "WHERE"   => "corpid='$corpid'"
    );