We were working on a finance project whose core need was storing, accessing and transmitting the financial data securely. Financial data includes user's bank account, transaction details, user's banks etc. If this data is exposed to the world, obvious effect will be misuse of the data. MySQL was used to perform the data access from database but to inefficient security of MySQL, we had to replace MySQL queries with PDO.
Benefits of PDO over MySQL:
1. Declaration and Calling
In Model:
Benefits of PDO over MySQL:
- Syntax are much cleaner (uses an object-oriented approach).
- It's cross database, meaning different rational databases can be addressed with the same interface
- It can protect SQL injections attack
1. Declaration and Calling
In Model:
- Make sure the base model is included and the model class is inherited from the base model:
require_once('base_model.php');
Class XX_model extends Base_Model - Calling the library as below:
$this->ci->library->
- Make sure the base controller is included and the controller class is
inherited from the base controller:
require_once(' base_controller.php');
Class XX extends Base_Controller - Calling the library as below:
$this->library->
- In function which need to call the library:
global $chyngedb; - Calling the library as below:
$chyngedb->;
- Select function to fetch records from database.
Format:
select ( SelectStruct $selStruct);
Description: Which it accept Class SelectStruct as parameter, hence a class object need to created before pass in as parameter. - SelectStruct to construct parameter for select function.
Declaration:
$selStruct = new SelectStruct( <tablename> );
- Execute query with return true or false as status. This is good for update, delete query
- Format:
a. execQuery($query, $arrParams=null , $isParamBind=false)
b. $query: query to be executed.
c. $arrParams: parameter to be passed into query
d. $isParamBind: either parameter in bind format or in sequence format
- Select return from tables, this is best use for query which unable to handle by select function, especially query which join multiple tables
- Format:
a. fetchAllData($query, $arrParams=array(), $isParamBind=false, $fetchMode=array(PDO::FETCH_OBJ,null,null))
b. $query: query to be executed.
c. $arrParams: parameter to be passed into query
d. $isParamBind: either parameter in bind format or in sequence format, by default is false
e. $fetchMode: fetchMode for select query, please refer to select function for more details.
- Insert new records into database table
- Format:
a. insert ($tablename, $ insertArray, $ isMultipleInsertion=false, & $errDBCode=null)
b. $tablename: target table
c. $insertArray: details of the record to be inserted, in key-value arrayformat.
d. $isMultipleInsertion: either more than one records to be inserted here, by default this is false. For the case this option set to true, $insertArray will need to be in multidimensional array format.
e. $errDBCode will return error code if failure happen
f. This function will return last inserted id, it will return false if the action failed.
- update 1 or more existing records in the database table
- Format:
a. update ($tablename, $ updateArray, $conditionArray, & $errDBCode=null)
b. $tablename: target table
c. $insertArray: details of the record to be updated, in key-value array format.
d. $conditionArray: details of the condition of record to be updated, in key-value array format.
e. $errDBCode will return error code if failure happen
f. This function will return true if success, false if failed
- delete 1 or more existing records in the database table
- Format:
a. delete ($tablename,, $conditionArray, & $errDBCode=null)
b. $tablename: target table
c. $conditionArray: details of the condition of record to be updated, in key-value array format.
d. $errDBCode will return error code if failure happen
e. This function will return true if success, false if failed
- escape string to prevent sql injection, can be used to escaped string forcolumn and table name out side of function.
- Format:
a. pdoEscape ($string)
b. $string: string to be escape
c. This function will return escaped string