My Database class
Workaround
/**
* Database Configuration Class
*/
Class Dbconfig
{
public static $HOST = ' localhost ';
public static $USERNAME = ' root ';
public static $PASSWORD = ' root ';
public static $DATABASE = ' shopping ';
public static $CHARSET = ' UTF8 ';
}
?>
Copy Code
/**
* Database Operation class
*/
Class database{
Private $connection;
/**
* Construction Method
* @access Public
*/
Public Function __construct () {
$CONFIG = require (DirName (__file__). ' /dbconfig.class.php ');
$this >connection = mysql_connect (dbconfig:: $HOST, Dbconfig:: $USERNAME, Dbconfig:: $PASSWORD);
mysql_select_db (dbconfig:: $DATABASE);
mysql_query ("SET NAMES"). Dbconfig:: $CHARSET. "'");
}
/**
* Destructor method
* @access Public
*/
Public Function __destruct () {
Mysql_close ($this >connection);
}
/**
* Execute SQL query statement
* @access Private
* @param string $p _sql query command
* @return Array Recordset, no records returned empty array
*/
Private Function query ($p _sql) {
$dataTemp = mysql_query ($p _sql, $this >connection);
$data = Array ();
$dataItem = 0;
while ($rows = Mysql_fetch_assoc ($dataTemp)) {
$data [$dataItem] = $rows;
$dataItem + +;
}
return $data;
}
/**
* Execute SQL statement
* @access Public
* @param string $p _sql need to execute SQL, can be insert,select,update or delete
* @return If SQL is SELECT, returns the recordset if SQL is INSERT, returns the new record ID if SQL is update or delete, returns the number of rows affected
*/
Public Function Execute ($p _sql) {
$CONTROLR = Strtoupper (substr ($p _sql,0,6));
Switch ($CONTROLR) {
Case ' INSERT ':
mysql_query ($p _sql, $this >connection);
$result = mysql_insert_id ($this >connection);
Break
Case ' SELECT ':
$result = $this >query ($p _sql, $this >connection);
Break
Default
mysql_query ($p _sql, $this >connection);
$result = Mysql_affected_rows ($this >connection);
Break
}
return $result;
}
}
?>
Copying code calls is simple:
$sql = ' ... '; can be any additions and deletions to change the statement
$db = new DataBase ();
$rs = $db >execute ($sql);
$DB = null;
Please give us a lot of advice!
[ ]
d8888d Huitie Content
Look at it ~ ~ [img]http://www.phpchina.com/bbs/images/smilies/default/shy.gif[/img][img]http://www.phpchina.com/bbs/ IMAGES/SMILIES/DEFAULT/SHY.GIF[/IMG]
http://www.bkjia.com/PHPjc/632535.html www.bkjia.com true http://www.bkjia.com/PHPjc/632535.html techarticle my Database class solution/** * DB Configuration class */class Dbconfig {public static $HOST = ' localhost ', public static $USERNAME = ' root ' ; public static $PASSWORD = ' root ';