我是借鉴了,ipb论坛的做法,不过不是很成熟,以新闻系统为例
主文件index.php如下:
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" ;
?>
require $root_path."sources/". $choice[ $IN['act']].".php";
通过相应的act到相应的文件执行程序
我常用的数据库类也是改自ipb
"" ,
"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 .= "\n\nmySQL 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 = "
```Invision Power Board Database Error
看起来我们的数据库出现了一点错误 { $INFO['board_name']} database.
你可以通过点击 此处来刷新本页, 如果问题依然存在, 你可以点击 此处来联系论坛管理员
Error Returned
We apologise for any inconvenience
```
" ;
/*
*/
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
再加上输入处理类
$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 ;