Use Singleton mode to implement mysql classes
- Defined ('ACC ') | exit ('Access Denied ');
- // Encapsulate the mysql operation class, including the connection and query functions.
- Class mysql extends absdb {
- Protected static $ ins = null;
- Protected $ host; // host name
- Protected $ user; // user name
- Protected $ passwd; // password
- Protected $ db; // database name
- Protected $ port; // port
- Protected $ conn = null;
- // Obtain an object through internal operations
- Public static function getIns (){
- If (self ::$ ins === null ){
- Self: $ ins = new self ();
- }
- $ Conf = conf: getIns ();
- Self: $ ins-> host = $ conf-> host;
- Self: $ ins-> user = $ conf-> user;
- Self: $ ins-> passwd = $ conf-> pwd;
- Self: $ ins-> db = $ conf-> db;
- Self: $ ins-> port = $ conf-> port;
- Self: $ ins-> connect ();
- Self: $ ins-> select_db ();
- Self: $ ins-> setChar ();
- Return self: $ ins;
- }
- // Do not allow external users to perform new operations,
- Protected function _ construct (){
-
- }
- // Connect to the database
- Public function connect (){
- $ This-> conn = @ mysql_connect ($ this-> host, $ this-> user, $ this-> passwd, $ this-> port );
- If (! $ This-> conn ){
- $ Error = new Exception ('database not connected', 9 );
- Throw $ error;
- }
- }
- // Send an SQL query
- Public function query ($ SQL ){
- $ Rs = mysql_query ($ SQL, $ this-> conn );
- If (! $ Rs ){
- Log: write ($ SQL );
- }
- Return $ rs;
- }
- // Encapsulate a getAll method
- // Parameter: $ SQL
- // Return value: array, false
- Public function getAll ($ SQL ){
- $ Rs = $ this-> query ($ SQL );
- If (! $ Rs ){
- Return false;
- }
- $ List = array ();
- While ($ row = mysql_fetch_assoc ($ rs )){
- $ List [] = $ row;
- }
- Return $ list;
-
- }
- // Encapsulate a getRow method
- // Parameter: $ SQL
- // Return value: array, false
- Public function getRow ($ SQL ){
- $ Rs = $ this-> query ($ SQL );
- If (! $ Rs ){
- Return false;
- }
- Return mysql_fetch_assoc ($ rs );
- }
- // Encapsulate a getOne method,
- // Parameter: $ SQL
- // Return value: int, str (single value)
- Public function getOne ($ SQL ){
- $ Rs = $ this-> query ($ SQL );
- If (! $ Rs ){
- Return false;
- }
- $ Tmp = mysql_fetch_row ($ rs );
- Return $ tmp [0];
- }
- // Encapsulate an afftect_rows () method
- // Parameter: None
- // Returns the number of affected int rows.
- Public function affected_rows (){
- Return mysql_affected_rows ($ this-> conn );
- }
- // Return the value of the newly generated auto_increment column
- Public function last_id (){
- Return mysql_insert_id ($ this-> conn );
- }
- // Select the database function
- Public function select_db (){
- $ SQL = 'use'. $ this-> db;
- Return $ this-> query ($ SQL );
- }
- // Set the character set function
- Public function setChar (){
- $ SQL = 'set names utf8 ';
- Return $ this-> query ($ SQL );
- }
- // Automatically generate the insert statement, update the statement, and execute
- Public function autoExecute ($ data, $ table, $ act = 'insert', $ where = ''){
-
- If ($ act = 'insert '){
- $ SQL = 'insert'. $ table .'(';
- $ SQL. = implode (',', (array_keys ($ data )));
- $ SQL. = ') values (\'';
- $ SQL. = implode ("','", array_values ($ data ));
- $ SQL. = "')";
- } Else if ($ act = 'update '){
- If (! Trim ($ where )){
- Return false;
- }
-
- $ SQL = 'update'. $ table. 'set ';
- Foreach ($ data as $ k => $ v ){
- $ SQL. = $ k;
- $ SQL. = ';
- $ SQL. = "'". $ v ."',";
- }
-
- $ SQL = substr ($ SQL, 0,-1 );
- $ SQL. = 'where ';
- $ SQL. = $ where;
- } Else {
- Return false;
- }
- // Return $ SQL;
- Return $ this-> query ($ SQL );
- }
- }
|
Mysql