var $query_id = ""; var $connection_id = ""; var $query_count = ""; var $record_row = array();
/*========================================================================*/ // Connect to the database /*========================================================================*/
/*========================================================================*/ // Process a query /*========================================================================*/
function query($the_query) {
//-------------------------------------- // Change the table prefix if needed //--------------------------------------
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; }
/*========================================================================*/ // Fetch a row based on the last query /*========================================================================*/
function fetch_row($query_id = "") {
if ($query_id == "") { $query_id = $this->query_id; }
} /*========================================================================*/ // Fetch the number of rows affected by the last query /*========================================================================*/
function get_affected_rows() { return mysql_affected_rows($this->connection_id); }
/*========================================================================*/ // Fetch the number of rows in a result set /*========================================================================*/
function get_num_rows() { return mysql_num_rows($this->query_id); }
/*========================================================================*/ // Fetch the last insert id from an sql autoincrement /*========================================================================*/
function get_insert_id() { return mysql_insert_id($this->connection_id); }
/*========================================================================*/ // Return the amount of queries used /*========================================================================*/
function get_query_cnt() { return $this->query_count; }
/*========================================================================*/ // Free the result set from mySQLs memory /*========================================================================*/
function free_result($query_id="") {
if ($query_id == "") { $query_id = $this->query_id; }
@mysql_free_result($query_id); }
/*========================================================================*/ // Shut down the database /*========================================================================*/
function close_db() { return mysql_close($this->connection_id); }
function fatal_error($the_error) { echo("Error: $the_error"); echo("<br><br>MySQL error: ". mysql_error() ); die (""); }
/*========================================================================*/ // Create an array from a multidimensional array returning formatted // strings ready to use in an INSERT query, saves having to manually format // the (INSERT INTO table) ('field', 'field', 'field') VALUES ('val', 'val') /*========================================================================*/
/*========================================================================*/ // Create an array from a multidimensional array returning a formatted // string ready to use in an UPDATE query, saves having to manually format // the FIELD='val', FIELD='val', FIELD='val' /*========================================================================*/
<?php /////////////////////////////////////////////////////////////////////////// function db_connect(){ GLOBAL $CONFIG; $CONFIG->dblink = mysql_connect($CONFIG->dbhost,$CONFIG->dbuser,$CONFIG->dbpass)or die("Unable to connect the database server");; mysql_select_db($CONFIG->dbname)or die("Unable to select the database");; }///////////////////////////////////////////////////////////////////////// function db_query($action=""){ GLOBAL $CONFIG; $qid=mysql_query($CONFIG->query,$CONFIG->dblink); if ($action=="select"){ return db_fetch_array($qid); }else{ return $qid; } }//////////////////////////////////////////////////////////////////////// function db_fetch_array($qid){ $counter=0; while ($row[$counter]=mysql_fetch_array($qid)){ $counter++; } mysql_free_result($qid); return $row; }/////////////////////////////////////////////////////////////////////// function db_field_type($tableName){ $qid=db_query("select * from $tableName limit 1"); }////////////////////////////////////////////////////////////////////// function db_close(){ GLOBAL $CONFIG; mysql_close($CONFIG->dblink); } ?> 使用: $CONFIG->query="insert into kehuyewu(kehuxuhao,yewuxuhao,yewujianchen,yewuzhuankuan,yewubody,currtime,changping,money,yewutype,shuliang) values ('".$form[kehuxuhao]."','".$form[yewuxuhao]."','".$form[yewujianchen]."','".$form[yewuzhuankuan]."','".$form[yewubody]."','".$datatime."','".$form[changping]."','".$form[money]."','".$form[yewutype]."','".$form[shuliang]."')"; db_query(); $CONFIG->query="update kehuyewu set yewujianchen='".$form[yewujianchen]."',yewuzhuankuan='".$form[yewuzhuankuan]."',yewubody='".$form[yewubody]."',changping='".$form[changping]."',money='".$form[money]."',yewutype='".$form[yewutype]."',shuliang='".$form[shuliang]."',currtime='".$datatime."' where kehuxuhao='".$form[kehuxuhao]."' and yewuxuhao='".$form[delmodfiy]."'"; db_query(); $CONFIG->query="select * from kehuwendang where kehuxuhao='".$form[kehuxuhao]."' and yewuxuhao='".$form[delmodfiy]."'"; $result=db_query("select");
忘了还有一些初始值 $CONFIG = new Setting; // Object for the dirctory struture $CONFIG->dbhost = "localhost"; $CONFIG->dbuser = "root"; $CONFIG->dbpass = ""; $CONFIG->dbname = "kl"; $CONFIG->dblink = ""; //database link identifier after db_connect() $CONFIG->query = "";
class db_driver { var $obj = array ( "sql_database" => "" ,
"sql_user" => "root" ,
"sql_pass" => "" ,
"sql_host" => "localhost",
"sql_port" => "" ,
"persistent" => "0" ,
"sql_tbl_prefix" => "ibf_" ,
"cached_queries" => array(),
);
var $query_id = "";
var $connection_id = "";
var $query_count = "";
var $record_row = array();
/*========================================================================*/
// Connect to the database
/*========================================================================*/
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']);
}
}
/*========================================================================*/
// Process a query
/*========================================================================*/
function query($the_query) {
//--------------------------------------
// Change the table prefix if needed
//--------------------------------------
if ($this->obj['sql_tbl_prefix'] != "ibf_")
{
$the_query = preg_replace("/ibf_(\S+?)([\s\.,]|$)/", $this->obj['sql_tbl_prefix']."\\1\\2", $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;
}
/*========================================================================*/
// Fetch a row based on the last query
/*========================================================================*/
function fetch_row($query_id = "") {
if ($query_id == "")
{
$query_id = $this->query_id;
}
$this->record_row = mysql_fetch_array($query_id, MYSQL_ASSOC);
return $this->record_row;
} /*========================================================================*/
// Fetch the number of rows affected by the last query
/*========================================================================*/
function get_affected_rows() {
return mysql_affected_rows($this->connection_id);
}
/*========================================================================*/
// Fetch the number of rows in a result set
/*========================================================================*/
function get_num_rows() {
return mysql_num_rows($this->query_id);
}
/*========================================================================*/
// Fetch the last insert id from an sql autoincrement
/*========================================================================*/
function get_insert_id() {
return mysql_insert_id($this->connection_id);
}
/*========================================================================*/
// Return the amount of queries used
/*========================================================================*/
function get_query_cnt() {
return $this->query_count;
}
/*========================================================================*/
// Free the result set from mySQLs memory
/*========================================================================*/
function free_result($query_id="") {
if ($query_id == "") {
$query_id = $this->query_id;
}
@mysql_free_result($query_id);
}
/*========================================================================*/
// Shut down the database
/*========================================================================*/
function close_db() {
return mysql_close($this->connection_id);
}
/*========================================================================*/
// Basic error handler
/*========================================================================*/
function fatal_error($the_error) {
echo("Error: $the_error");
echo("<br><br>MySQL error: ". mysql_error() );
die ("");
}
/*========================================================================*/
// Create an array from a multidimensional array returning formatted
// strings ready to use in an INSERT query, saves having to manually format
// the (INSERT INTO table) ('field', 'field', 'field') VALUES ('val', 'val')
/*========================================================================*/
function compile_db_insert_string($data) {
$field_names = "";
$field_values = "";
foreach ($data as $k => $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,
);
}
/*========================================================================*/
// Create an array from a multidimensional array returning a formatted
// string ready to use in an UPDATE query, saves having to manually format
// the FIELD='val', FIELD='val', FIELD='val'
/*========================================================================*/
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;
}
} // end class
?>
///////////////////////////////////////////////////////////////////////////
function db_connect(){
GLOBAL $CONFIG;
$CONFIG->dblink = mysql_connect($CONFIG->dbhost,$CONFIG->dbuser,$CONFIG->dbpass)or die("Unable to connect the database server");;
mysql_select_db($CONFIG->dbname)or die("Unable to select the database");;
}/////////////////////////////////////////////////////////////////////////
function db_query($action=""){
GLOBAL $CONFIG;
$qid=mysql_query($CONFIG->query,$CONFIG->dblink);
if ($action=="select"){
return db_fetch_array($qid);
}else{
return $qid;
}
}////////////////////////////////////////////////////////////////////////
function db_fetch_array($qid){
$counter=0;
while ($row[$counter]=mysql_fetch_array($qid)){
$counter++;
}
mysql_free_result($qid);
return $row;
}///////////////////////////////////////////////////////////////////////
function db_field_type($tableName){
$qid=db_query("select * from $tableName limit 1");
}//////////////////////////////////////////////////////////////////////
function db_close(){
GLOBAL $CONFIG;
mysql_close($CONFIG->dblink);
}
?>
使用:
$CONFIG->query="insert into kehuyewu(kehuxuhao,yewuxuhao,yewujianchen,yewuzhuankuan,yewubody,currtime,changping,money,yewutype,shuliang) values ('".$form[kehuxuhao]."','".$form[yewuxuhao]."','".$form[yewujianchen]."','".$form[yewuzhuankuan]."','".$form[yewubody]."','".$datatime."','".$form[changping]."','".$form[money]."','".$form[yewutype]."','".$form[shuliang]."')";
db_query(); $CONFIG->query="update kehuyewu set yewujianchen='".$form[yewujianchen]."',yewuzhuankuan='".$form[yewuzhuankuan]."',yewubody='".$form[yewubody]."',changping='".$form[changping]."',money='".$form[money]."',yewutype='".$form[yewutype]."',shuliang='".$form[shuliang]."',currtime='".$datatime."' where kehuxuhao='".$form[kehuxuhao]."' and yewuxuhao='".$form[delmodfiy]."'";
db_query(); $CONFIG->query="select * from kehuwendang where kehuxuhao='".$form[kehuxuhao]."' and yewuxuhao='".$form[delmodfiy]."'";
$result=db_query("select");
$CONFIG = new Setting; // Object for the dirctory struture
$CONFIG->dbhost = "localhost";
$CONFIG->dbuser = "root";
$CONFIG->dbpass = "";
$CONFIG->dbname = "kl";
$CONFIG->dblink = ""; //database link identifier after db_connect()
$CONFIG->query = "";