A phppdomysql operation class
-
/**
- * PDO operations
- * Created by PhpStorm.
- * User: sumiaowen
- * Date: 13-11-28
- * Time: PM
- * To change this template use File | Settings | File Templates.
- */
- Class Pdo_db
- {
- Private $ dns = null;
- Private $ username = null;
- Private $ password = null;
- Private $ conn = null;
- Private static $ _ instance = null;
-
Private function _ construct ($ params = array ())
- {
- $ This-> dns = $ params ['dns'];
- $ This-> username = $ params ['username'];
- $ This-> password = $ params ['password'];
-
$ This-> _ connect ();
- }
-
Private function _ clone (){}
-
Public function get_instance ($ params = array ())
- {
- If (! (Self: $ _ instance instanceof self ))
- {
- Self: $ _ instance = new self ($ params );
- }
-
Return self: $ _ instance;
- }
-
Private function _ connect ()
- {
- Try
- {
- $ This-> conn = new PDO ($ this-> dns, $ this-> username, $ this-> password );
- $ This-> conn-> query ('set names utf8 ');
- }
- Catch (PDOException $ e)
- {
- Exit ('pdoexception: '. $ e-> getMessage ());
- }
- }
-
/**
- * Query an SQL statement
- * @ Param string $ SQL
- * @ Param array $ parameters to be bound
- * @ Param int $ option
- * @ Return array
- */
- Public function query ($ SQL, $ parameters = array (), $ option = PDO: FETCH_ASSOC)
- {
- $ Stmt = $ this-> conn-> prepare ($ SQL );
- $ Stmt-> execute ($ parameters );
-
$ Tmp = array ();
- While ($ row = $ stmt-> fetch ($ option ))
- {
- $ Tmp [] = $ row;
- }
-
Return $ tmp;
- }
-
/**
- * Insert a data entry
- * @ Param string $ SQL
- * @ Param array $ parameters
- * @ Return int 1 or 0 returns the number of affected rows
- */
- Public function insert ($ SQL, $ parameters = array ())
- {
- $ Stmt = $ this-> conn-> prepare ($ SQL );
- $ Stmt-> execute ($ parameters );
-
Return $ stmt-> rowCount ();
- }
-
/**
- * Update a piece of data
- * @ Param string $ SQL
- * @ Param array $ parameters
- * @ Return int 1 or 0 returns the number of affected rows
- */Bbs.it-home.org
- Public function update ($ SQL, $ parameters = array ())
- {
- $ Stmt = $ this-> conn-> prepare ($ SQL );
- $ Stmt-> execute ($ parameters );
-
Return $ stmt-> rowCount ();
- }
-
/**
- * Delete a piece of data
- * @ Param string $ SQL
- * @ Param array $ parameters
- * @ Return int 1 or 0 returns the number of affected rows
- */
- Public function delete ($ SQL, $ parameters = array ())
- {
- $ Stmt = $ this-> conn-> prepare ($ SQL );
- $ Stmt-> execute ($ parameters );
-
Return $ stmt-> rowCount ();
- }
- }
Code link: https://github.com/sumiaowen/mynotes/tree/master/pdo_mysql_class |