Php pdo database operations
A simple PDO class encapsulation .. Learning and communication only PdoDb database class
- /**
- * @ Throws Error
- * PDO database
- */
- Class PdoDb extends DatabaseAbstract
- {
- /**
- * PDO instance
- * @ Var PDO
- */
- Protected $ DB;
- /**
- * PDO preparation statement
- * @ Var PDOStatement
- */
- Protected $ Stmt;
- /**
- * The final SQL statement
- * @ Var string
- */
- Protected $ SQL;
- /**
- * Configuration information $ config = array ('dsn '=> xxx, 'name' => xxx, 'password' => xxx, 'option' => xxx)
- * @ Var array
- */
- Protected $ Config;
- /**
- * Constructor
- * @ Param array $ config
- */
- Public function _ construct ($ config)
- {
- $ This-> Config = $ config;
- }
- /**
- * Connect to the database
- * @ Return void
- */
- Public function connect ()
- {
- $ This-> DB = new PDO ($ this-> Config ['dsn '], $ this-> Config ['name'], $ this-> Config ['password'], $ this-> Config ['option']);
- // Serialize the result to stdClass by default
- $ This-> DB-> setAttribute (PDO: ATTR_DEFAULT_FETCH_MODE, PDO: FETCH_OBJ );
- // Write your own code to catch Exception
- $ This-> DB-> setAttribute (PDO: ATTR_ERRMODE, PDO: ERRMODE_SILENT );
- }
- /**
- * Disconnect
- * @ Return void
- */
- Public function disConnect ()
- {
- $ This-> DB = null;
- $ This-> Stmt = null;
- }
- /**
- * Execute the SQL statement and return the newly added id.
- * @ Param string $ statement
- * @ Return string
- */
- Public function exec ($ statement)
- {
- If ($ this-> DB-> exec ($ statement )){
- $ This-> SQL = $ statement;
- Return $ this-> lastId ();
- }
- $ This-> errorMessage ();
- }
- /**
- * Querying SQL
- * @ Param string $ statement
- * @ Return PdoDb
- */
- Public function query ($ statement)
- {
- $ Res = $ this-> DB-> query ($ statement );
- If ($ res ){
- $ This-> Stmt = $ res;
- $ This-> SQL = $ statement;
- Return $ this;
- }
- $ This-> errorMessage ();
- }
- /**
- * Serialize data once
- * @ Return mixed
- */
- Public function fetchOne ()
- {
- Return $ this-> Stmt-> fetch ();
- }
- /**
- * Serialize all data
- * @ Return array
- */
- Public function fetchAll ()
- {
- Return $ this-> Stmt-> fetchAll ();
- }
- /**
- * Last added id
- * @ Return string
- */
- Public function lastId ()
- {
- Return $ this-> DB-> lastInsertId ();
- }
- /**
- * Number of affected rows
- * @ Return int
- */
- Public function affectRows ()
- {
- Return $ this-> Stmt-> rowCount ();
- }
- /**
- * Prepared statement
- * @ Param string $ statement
- * @ Return PdoDb
- */
- Public function prepare ($ statement)
- {
- $ Res = $ this-> DB-> prepare ($ statement );
- If ($ res ){
- $ This-> Stmt = $ res;
- $ This-> SQL = $ statement;
- Return $ this;
- }
- $ This-> errorMessage ();
- }
- /**
- * Bind data
- * @ Param array $ array
- * @ Return PdoDb
- */
- Public function bindArray ($ array)
- {
- Foreach ($ array as $ k => $ v ){
- If (is_array ($ v )){
- // Effective structure of array ('value' => xxx, 'type' => PDO: PARAM_XXX)
- $ This-> Stmt-> bindValue ($ k + 1, $ v ['value'], $ v ['type']);
- } Else {
- $ This-> Stmt-> bindValue ($ k + 1, $ v, PDO: PARAM_STR );
- }
- }
- Return $ this;
- }
- /**
- * Execute the prepared statement
- * @ Return bool
- */
- Public function execute ()
- {
- If ($ this-> Stmt-> execute ()){
- Return true;
- }
- $ This-> errorMessage ();
- }
- /**
- * Start a transaction
- * @ Return bool
- */
- Public function beginTransaction ()
- {
- Return $ this-> DB-> beginTransaction ();
- }
- /**
- * Execute transactions
- * @ Return bool
- */
- Public function commitTransaction ()
- {
- Return $ this-> DB-> commit ();
- }
- /**
- * Roll back a transaction
- * @ Return bool
- */
- Public function rollbackTransaction ()
- {
- Return $ this-> DB-> rollBack ();
- }
- /**
- * Throw an error.
- * @ Throws Error
- * @ Return void
- */
- Public function errorMessage ()
- {
- $ Msg = $ this-> DB-> errorInfo ();
- Throw new Error ('database Error: '. $ msg [2]);
- }
- //---------------------
- /**
- * Singleton instance
- * @ Var PdoDb
- */
- Protected static $ _ instance;
- /**
- * Default database
- * @ Static
- * @ Param array $ config
- * @ Return PdoDb
- */
- Public static function instance ($ config)
- {
- If (! Self: $ _ instance instanceof PdoDb ){
- Self: $ _ instance = new PdoDb ($ config );
- Self: $ _ instance-> connect ();
- }
- Return self: $ _ instance;
- }
- //----------------------
- /**
- * Obtain databases supported by PDO
- * @ Static
- * @ Return array
- */
- Public static function getSupportDriver (){
- Return PDO: getAvailableDrivers ();
- }
- /**
- * Obtain the database version information.
- * @ Return array
- */
- Public function getDriverVersion (){
- $ Name = $ this-> DB-> getAttribute (PDO: ATTR_DRIVER_NAME );
- Return array ($ name => $ this-> DB-> getAttribute (PDO: ATTR_CLIENT_VERSION ));
- }
- }
When using
- PdoDb: instance ($ config );
|
PHP, PDO