date_default_timezone_set('Asia/Shanghai');define('DEBUG',$_SERVER['REMOTE_ADDR']=='127.0.0.1'?true:false);define('DB_AUTO_FREE',true);define('TABLE_PREFIX','my_');// Mysql & LOG_MSGif(DEBUG){define('CONNECTION_STR','mysql://db_user:db_password@localhost:3306/db_development_name');define('LOG_MSG','msg.txt');}else{define('CONNECTION_STR','mysql://db_user:db_password@localhost:3306/db_product_name');}functionlogger($msg){if(defined('LOG_MSG')&&LOG_MSG){//if ($file=fopen(LOG_MSG, 'a')) fwrite($file, $msg."\n"); fclose($file);file_put_contents(LOG_MSG,$msg."\n",FILE_APPEND|LOCK_EX);}}$tables=array('attachs','metas','nodes','nodes_text','taged','tags');foreach($tablesas$name){define("table_".$name,TABLE_PREFIX.$name);${"table_".$name}=TABLE_PREFIX.$name;}functiondb_connect(){static$connect_id;if(!is_resource($connect_id)){if(!defined('CONNECTION_STR'))trigger_error('Connect() - CONNECTION_STR is undefined',E_USER_ERROR);$dsn=parse_url(CONNECTION_STR)ortrigger_error("Connect() - could not parse connection string:".CONNECTION_STR,E_USER_ERROR);$connect_id=mysql_connect($dsn['host'].':'.$dsn['port'],$dsn['user'],$dsn['pass'])ortrigger_error('Connection - could not connect to database Server:'.$dsn['host'],E_USER_ERROR);mysql_select_db(str_replace('/','',$dsn['path']),$connect_id)ortrigger_error('Connect() - could not select Database: '.$dsn['path'],E_USER_ERROR);mysql_query('SET NAMES UTF8',$connect_id);}return$connect_id;}//$sql Model::Prepare("SELECT * FROM person WHERE last = '%s' AND age=%d", $_GET['lastname'], $_GET['age']);// FindBySql('Person', $sql);functiondb_prepare(){$args=func_get_args();$rawsql=array_shift($args);if(get_magic_quotes_gpc())$args=array_map('stripslashes',$args);$sql=vsprintf($rawsql,$args);return$sql;}functiondb_query($sql){//log queryif(defined('LOG_SQL')&&LOG_SQL){if($file=fopen(LOG_SQL,'a'))fwrite($file,$sql."\n");}if($result=mysql_query($sql,db_connect())){return$result;}else{trigger_error('Query - query failed:'.$sql.' with error:'.mysql_error(db_connect()),E_USER_WARNING);returnfalse;}}functiondb_row($sql,$limit=true){if($limit)$sql.=' LIMIT 1';$query_id=db_query($sql);//if(!$queryId) trigger_error('Query - query failed:'.$sql.' with error:'.mysql_error(Connect()), E_USER_ERROR);$rs=mysql_fetch_array($query_id,MYSQL_ASSOC);if(!$rs)returnfalse;if(defined(DB_AUTO_FREE)&&DB_AUTO_FREE)mysql_free_result($query_id);return(count($rs)==1)?current($rs):$rs;}functiondb_create($table,$values){foreach($valuesas$key=>$value){$keys[]=$key;$insertvalues[]='\''.$value.'\'';}$keys=implode(',',$keys);$insertvalues=implode(',',$insertvalues);$sql="INSERT INTO $table ($keys) VALUES ($insertvalues);";db_query($sql);returnmysql_insert_id(db_connect());}functiondb_update($table,$update_values,$filter=''){foreach($update_valuesas$key=>$value)$sets[]=$key.'=\''.$value.'\'';$sets=implode(',',$sets);if($filter)$where_condition=" WHERE ".$filter;$query="UPDATE $table SET $sets$where_condition";db_query($query);returndb_affected();}functiondb_remove($table,$filter){$sql="DELETE FROM $table";if($filter)$sql.=" WHERE $filter";returndb_query($sql);}functiondb_fetch($sql,$begin_row=0,$limit=0){$data=array();if($begin_row)$begin_row=$begin_row.',';if($limit)$sql.=' LIMIT '.$begin_row.$limit;$query_id=db_query($sql);//if(!$queryId) trigger_error('Query - Invalid SQL:'.$sql.' with error:'.mysql_error(Connect()), E_USER_WARNING);while($row=mysql_fetch_array($query_id,MYSQL_ASSOC)){//$array[]=$thisRow;array_push($data,$row);}if(defined(DB_AUTO_FREE)&&DB_AUTO_FREE)mysql_free_result($query_id);return$data;}functiondb_affected(){mysql_affected_rows(db_connect());}functiondb_close(){mysql_close(db_connect());}