能否介绍一下大家在开发项目中用什么样的结构
我是借鉴了,ipb论坛的做法,不过不是很成熟,以新闻系统为例 主文件index.php如下: <? $root_path = "./"; error_reporting (E_ERROR | E_WARNING | E_PARSE); set_magic_quotes_runtime(1); require $root_path."sources/Drivers/input.php"; require $root_path."sources/Drivers/functions.php"; require $root_path."sources/Drivers/Exception.php"; require $root_path."conf_global.php"; $std = new Input; $IN = $std->parse_incoming();
$to_require = $root_path."sources/Drivers/".$INFO['sql_driver'].".php"; require ($to_require); $DB = new db_driver;
$DB->obj['sql_database'] = $INFO['sql_database']; $DB->obj['sql_user'] = $INFO['sql_user']; $DB->obj['sql_pass'] = $INFO['sql_pass']; $DB->obj['sql_host'] = $INFO['sql_host']; $DB->connect(); session_start(); $member=isLogin(); if(!$member) require $root_path."sources/Login.php"; $choice = array( "idx" => "newslist", "newsadd" => "newsadd", "newsedit" => "newsedit", "newsdel" => "del", "make" => "makehtml", "changepass" => "change_pass", "Login" => "Login"
); $IN['act'] = $IN['act'] == '' ? "idx" : $IN['act']; if (! isset($choice[$IN['act']]) ) { $IN['act'] = 'idx'; }
require $root_path."sources/Drivers/template.php"; require $root_path."sources/Drivers/page.php"; require $root_path."sources/Drivers/pfcOutputStream.php"; require $root_path."sources/".$choice[$IN['act']].".php"; ?> 我常用的数据库类也是改自ipb <?php <? class db_driver {
var $obj = array ( "sql_database" => "" , "sql_user" => "root" , "sql_pass" => "" , "sql_host" => "localhost", "sql_port" => "" , "persistent" => "0" , "cached_queries" => array(), ); var $query_id = ""; var $connection_id = ""; var $query_count = 0; var $record_row = array(); var $record_set = array(); var $return_die = 0; var $error = "";
function connect() { if ($this->obj['persistent']) { $this->connection_id = mysql_pconnect( $this->obj['sql_host'] , $this->obj['sql_user'] , $this->obj['sql_pass'] ); } else { $this->connection_id = mysql_connect( $this->obj['sql_host'] , $this->obj['sql_user'] , $this->obj['sql_pass'] ); } if ( !mysql_select_db($this->obj['sql_database'], $this->connection_id) ) { echo ("ERROR: Cannot find database ".$this->obj['sql_database']); } }
function executeQuery($the_query) { $this->record_set = array(); $this->query_id = mysql_query($the_query, $this->connection_id); if (! $this->query_id ) { $this->fatal_error("mySQL query error: $the_query"); } $this->query_count++; $this->obj['cached_queries'][] = $the_query; while($this->record_row = mysql_fetch_array($this->query_id, MYSQL_ASSOC)) { $this->record_set[]=$this->record_row; } return $this->record_set; }
function executeUpdate($the_query) { $this->query_id = mysql_query($the_query, $this->connection_id); if (! $this->query_id ) { $this->fatal_error("mySQL query error: $the_query"); } $this->query_count++; $this->obj['cached_queries'][] = $the_query; return $this->query_id; }
function get_affected_rows() { return mysql_affected_rows($this->connection_id); }
function get_num_rows() { return mysql_num_rows($this->query_id); }
function get_insert_id() { return mysql_insert_id($this->connection_id); }
function get_query_cnt() { return $this->query_count; }
function free_result($query_id="") { if ($query_id == "") { $query_id = $this->query_id; } @mysql_free_result($query_id); }
function close_db() { return mysql_close($this->connection_id); }
function get_table_names() { $result = mysql_list_tables($this->obj['sql_database']); $num_tables = @mysql_numrows($result); for ($i = 0; $i < $num_tables; $i++) { $tables[] = mysql_tablename($result, $i); } mysql_free_result($result); return $tables; } function get_result_fields($query_id="") { if ($query_id == "") { $query_id = $this->query_id; } while ($field = mysql_fetch_field($query_id)) { $Fields[] = $field; } //mysql_free_result($query_id); return $Fields; } function fatal_error($the_error) { global $INFO; // Are we simply returning the error? if ($this->return_die == 1) { $this->error = mysql_error(); $this->error_no = mysql_errno(); $this->failed = 1; return; } $the_error .= "nnmySQL error: ".mysql_error()."n"; $the_error .= "mySQL error code: ".$this->error_no."n"; $the_error .= "Date: ".date("l dS of F Y h:i:s A"); $out = "<html><head><title>Invision Power Board Database Error</title> <style>P,BODY{ font-family:arial,sans-serif; font-size:11px; }</style></head><body> <br><br><blockquote><b>看起来我们的数据库出现了一点错误 {$INFO['board_name']} database.</b><br> 你可以通过点击 <a href="javascript:window.location=window.location;">此处</a>来刷新本页, 如果问题依然存在, 你可以点击 <a href='mailto:{$INFO['email_in']}?subject=SQL+Error'>此处</a>来联系论坛管理员 <br><br><b>Error Returned</b><br> <br>We apologise for any inconvenience</blockquote></body></html>"; /*<!--<form name='mysql'><textarea rows="15" cols="60">".htmlspecialchars($the_error)."</textarea></form> --> */ echo($out); die(""); } function compile_db_insert_string($data) { $field_names = ""; $field_values = ""; foreach ($data as $k => $v) { $v = preg_replace( "/'/", "'", $v ); //$v = preg_replace( "/#/", "#", $v ); $field_names .= "$k,"; $field_values .= "'$v',"; } $field_names = preg_replace( "/,$/" , "" , $field_names ); $field_values = preg_replace( "/,$/" , "" , $field_values ); return array( 'FIELD_NAMES' => $field_names, 'FIELD_VALUES' => $field_values, ); }
function compile_db_update_string($data) { $return_string = ""; foreach ($data as $k => $v) { $v = preg_replace( "/'/", "'", $v ); $return_string .= $k . "='".$v."',"; } $return_string = preg_replace( "/,$/" , "" , $return_string ); return $return_string; } function field_exists($field, $table) { $this->return_die = 1; $this->error = ""; $this->query("SELECT COUNT($field) as count FROM $table"); $return = 1; if ( $this->failed ) { $return = 0; } $this->error = ""; $this->return_die = 0; $this->error_no = 0; $this->failed = 0; return $return; } } // end class ?> 比较明显改动的地方是executeQuery和executeUpdate,有点象java 当select的时候返回的 是结果集数组 当update,del,insert使,返回true和false 再加上输入处理类 <? class Input { function clean_value($val) { //$val=trim($val); if ($val == "") { return ""; } if(!Get_Magic_Quotes_GPC()) { $val = addslashes($val); } // Swop PHP added backslashes //$val=chop($val); return $val; } function clean_key($key) { if ($key == "") { return ""; } $key = preg_replace( "/../" , "" , $key ); $key = preg_replace( "/__(.+?)__/" , "" , $key ); $key = preg_replace( "/^([w.-_]+)$/", "$1", $key ); return $key; } function select_var($array) { if ( !is_array($array) ) return -1; ksort($array); $chosen = -1; // Ensure that we return zero if nothing else is available foreach ($array as $k => $v) { if (isset($v)) { $chosen = $v; break; } } return $chosen; } function parse_incoming() { global $HTTP_X_FORWARDED_FOR, $HTTP_PROXY_USER, $HTTP_CLIENT_IP; $this->get_magic_quotes = get_magic_quotes_gpc(); $return = array(); if( is_array($_GET) ) { while( list($k, $v) = each($_GET) ) { if ( is_array($_GET[$k]) ) { while( list($k2, $v2) = each($_GET[$k]) ) { $return[ $this->clean_key($k) ][ $this->clean_key($k2) ] = $this->clean_value($v2); } } else { $return[ $this->clean_key($k) ] = $this->clean_value($v); } } } //---------------------------------------- // Overwrite GET data with post data //---------------------------------------- if( is_array($_POST) ) { while( list($k, $v) = each($_POST) ) { if ( is_array($_POST[$k]) ) { while( list($k2, $v2) = each($_POST[$k]) ) { $return[ $this->clean_key($k) ][ $this->clean_key($k2) ] = $this->clean_value($v2); } } else { $return[ $this->clean_key($k) ] = $this->clean_value($v); } } } //---------------------------------------- // Sort out the accessing IP // (Thanks to Cosmos and schickb) //---------------------------------------- $addrs = array(); foreach( array_reverse( explode( ',', $HTTP_X_FORWARDED_FOR ) ) as $x_f ) { $x_f = trim($x_f); if ( preg_match( '/^d{1,3}.d{1,3}.d{1,3}.d{1,3}$/', $x_f ) ) { $addrs[] = $x_f; } } $addrs[] = $_SERVER['REMOTE_ADDR']; $addrs[] = $HTTP_PROXY_USER; $addrs[] = $HTTP_CLIENT_IP; $return['IP_ADDRESS'] = $this->select_var( $addrs ); // Make sure we take a valid IP address $return['IP_ADDRESS'] = preg_replace( "/^([0-9]{1,3}).([0-9]{1,3}).([0-9]{1,3}).([0-9]{1,3})/", "1.2.3.4", $return['IP_ADDRESS'] ); $return['request_method'] = strtolower($_SERVER['REQUEST_METHOD']); return $return; } }
?>
|