项目作者: gurkanbicer

项目描述 :
Simple PHP Pdo Database Class
高级语言: PHP
项目地址: git://github.com/gurkanbicer/spdo.git
创建时间: 2019-05-05T20:46:30Z
项目社区:https://github.com/gurkanbicer/spdo

开源协议:

下载


Spdo

Simple PDO Database Class

  • Less coding
  • PDO-Based Structure
  • Multiple Database Connection Possiblity
  • Multiple Output Possiblity (json, xml, object, array)
  • CodeIgniter 2.x and 3.x Support

You must download Spdo.php file from Github servers to your project directory. After downloading, you must create a database connection configuration file.

Sample Database Connection Configuration:

  1. $db = [
  2. "default" => [
  3. "database" => "YourDatabaseName",
  4. "hostname" => "MysqlHostname",
  5. "username" => "MysqlUsername",
  6. "password" => "MysqlUserPassword",
  7. "char_set" => "utf8",
  8. ],
  9. "secondaryDB" => [
  10. "database" => "YourDatabaseName",
  11. "hostname" => "MysqlHostname",
  12. "username" => "MysqlUsername",
  13. "password" => "MysqlUserPassword",
  14. "char_set" => "utf8",
  15. ]
  16. ];

Maybe, you want to include this codes to your own configuration file. No problem, just this codes must be before Spdo class.

Sample Including Spdo Class To Project:

  1. include "config.php";
  2. include "Spdo.php";
  3. $spdo = new Spdo();

If you want this class to CodeIgniter project, you must follow this rules;

  • Add a value to $autoload variable like as this;
  1. $autoload['libraries'] = array('Spdo');
  • You must move Spdo.php file to application/libraries directory.
  • Open Spdo.php file and apply like as this changes;
  1. #require_once APPPATH . 'config/database.php';

to

  1. require_once APPPATH . 'config/database.php';
  • Open application/config/database.php and edit database configuration values for your database.

Functions List:

  • errors()
  • getErrors()
  • getLastQuery()
  • numRows()
  • getResults()
  • getRow()
  • getVar()
  • execute()
  • insert()
  • update()
  • delete()

Some Sample For CRUD Processes:

Using getResults() function

  1. $results = $spdo->getResults('SELECT * FROM categories');

Returning data type: object

  1. $results = $spdo->getResults('SELECT * FROM categories', ["returnDataType" => "json"]);

Returning data type: json
returnDataType parameter’s value can be object, array, json or xml.

  1. $results = $spdo->getResults('SELECT * FROM categories WHERE status > ?',
  2. ["bindValues" => ["active"], "returnDataType" => "json"]);

Returning data type: json
bindValues parameter’s value can be array or string.

  1. $results = $spdo->getResults('SELECT * FROM categories WHERE status > ?',
  2. ["bindValues" => ["active"], "returnDataType" => "json", "configKey" => "secondaryDB"]);

Returning data type: json
configKey parameter’s value can be a parameter name on the your config file.

Using getRow() function

  1. $result = $spdo->getRow('SELECT * FROM categories ID = 5');

Returning data type: object

  1. $result = $spdo->getRow('SELECT * FROM categories ID = 5', array("returnDataType" => "json"));

Returning data type: json
returnDataType parameter’s value can be object, array, json or xml.

  1. $result = $spdo->getRow('SELECT * FROM categories WHERE name = ?', array("bindValues" => array("Technology"), "returnDataType" => "json"));

Returning data type: json
bindValues parameter’s value can be array or string.

  1. $result = $spdo->getRow('SELECT * FROM categories WHERE name = ?', array("bindValues" => array("Technology"), "returnDataType" => "json", "configKey" => "secondaryDB"));

Returning data type: json
configKey parameter’s value can be a parameter name on the your config file.

Using getVar() function

  1. $categoryName = $spdo->getVar('SELECT name FROM categories ID = 5');

Returning data type: string

  1. $categoryName = $spdo->getVar('SELECT name FROM categories ID = 5', array("returnDataType" => "json"));

Returning data type: json
returnDataType parameter’s value can be object, array, json or xml

  1. $categoryStatus = $spdo->getVar('SELECT status FROM categories WHERE name = ?', array("bindValues" => array("Technology"), "returnDataType" => "json"));

Returning data type:
bindValues parameter’s value can be array or string.

  1. $categoryStatus = $spdo->getVar('SELECT status FROM categories WHERE name = ?', array("bindValues" => array("Technology"), "returnDataType" => "json", "configKey" => "secondaryDB"));

Returning data type.
configKey parameter’s value can be a parameter name on the your config file.

Using insert() function

  1. $category = array('name' => 'Technology', 'status' => 'active');
  2. $result = $spdo->insert('categories', $category);

Returning data: Inserted Row Id
First parameter: table name
Secondary paramater: column names and values

  1. $category = array('name' => 'Technology', 'status' => 'active');
  2. $result = $spdo->insert('categories', $category, array('configKey' => 'secondaryDB'));

Returning data: Inserted row id
First parameter: table name
Secondary paramater: column names and values
configKey paramater’s can be a parameter name on the config file.

Using update() function

  1. $result = $spdo->update('categories', array('status' => 'active'), array('ID > ?'), array(5));

Returning data: Affected row number
First parameter: table name
Secondary parameter: column names and values
Third parameter: Where block
Fourth parameter: Where block values

  1. $result = $spdo->update('categories', array('status' => 'active'), array('ID > ?'), array(5), array('configKey' => 'secondaryDB'));

Returning data: Affected row number
First parameter: table name
Secondary parameter: column names and values
Third parameter: Where block
Fourth parameter: Where block values
configKey parameter’s can be a paramater name on the config file.

Using delete() function

  1. $result = $spdo->delete('categories', array('ID = 5'));

First paramater: table name
Secondary paramater: where block

  1. $result = $spdo->delete('categories', array('ID = ?'), array(5));

First paramater: table name
Secondary parameter: where block
Third parameter: where block values

  1. $result = $spdo->delete('categories', array('ID = ?'), array(5), array('configKey' => 'secondaryDB'));

First paramater: table name
Secondary parameter: where block
Third parameter: where block values
configKey value can be a parameter name on the config file.

Using execute() function

Samples are in the below:

Sample 1:

  1. $result = $spdo->execute('INSERT INTO categories SET name = ?, status = ?', array('bindValues' => array('Technology', 'status')));

Sample 2:

  1. $result = $spdo->execute('INSERT INTO categories SET name = ?, status = ?', array('bindValues' => array('Technology', 'active'), 'configKey' => 'secondaryDB'));

Sample 3:

  1. $result = $spdo->execute('DELETE FROM categories WHERE ID = ?', array('bindValues' => array(5)));

Sample 4:

  1. $result = $spdo->execute('DELETE FROM categories WHERE ID = ?', array('bindValues' => array(5), 'configKey' => 'secondaryDB'));

Sample 5:

  1. $result = $spdo->execute('UPDATE categories SET name = ? WHERE ID = ?', array('bindValues' => array('Tech', 5)));

Sample 6

  1. $result = $spdo->execute('UPDATE categories SET name = ? WHERE ID = ?', array('bindValues' => array('Tech', 5), 'configKey' => 'secondaryDB'));

Other functions

  1. $spdo->errors();

This function shows all errors.

  1. $errors = $spdo->getErrors();

This function returns all errors.

  1. $spdo->getLastQuery();

This function will return data as following;

  • SQL query,
  • Binding values,
  • Selected database,
  • Returned data type,
  1. echo $spdo->numRows();

This function returns selected rows in the SELECT query.

If you’re have a question, please send an email to gurkan@grkn.co

Gurkan Bicer