Simple PHP Pdo Database Class
Simple PDO Database Class
You must download Spdo.php file from Github servers to your project directory. After downloading, you must create a database connection configuration file.
$db = [
"default" => [
"database" => "YourDatabaseName",
"hostname" => "MysqlHostname",
"username" => "MysqlUsername",
"password" => "MysqlUserPassword",
"char_set" => "utf8",
],
"secondaryDB" => [
"database" => "YourDatabaseName",
"hostname" => "MysqlHostname",
"username" => "MysqlUsername",
"password" => "MysqlUserPassword",
"char_set" => "utf8",
]
];
Maybe, you want to include this codes to your own configuration file. No problem, just this codes must be before Spdo class.
include "config.php";
include "Spdo.php";
$spdo = new Spdo();
If you want this class to CodeIgniter project, you must follow this rules;
$autoload['libraries'] = array('Spdo');
#require_once APPPATH . 'config/database.php';
to
require_once APPPATH . 'config/database.php';
$results = $spdo->getResults('SELECT * FROM categories');
Returning data type: object
$results = $spdo->getResults('SELECT * FROM categories', ["returnDataType" => "json"]);
Returning data type: json
returnDataType parameter’s value can be object, array, json or xml.
$results = $spdo->getResults('SELECT * FROM categories WHERE status > ?',
["bindValues" => ["active"], "returnDataType" => "json"]);
Returning data type: json
bindValues parameter’s value can be array or string.
$results = $spdo->getResults('SELECT * FROM categories WHERE status > ?',
["bindValues" => ["active"], "returnDataType" => "json", "configKey" => "secondaryDB"]);
Returning data type: json
configKey parameter’s value can be a parameter name on the your config file.
$result = $spdo->getRow('SELECT * FROM categories ID = 5');
Returning data type: object
$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.
$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.
$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.
$categoryName = $spdo->getVar('SELECT name FROM categories ID = 5');
Returning data type: string
$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
$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.
$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.
$category = array('name' => 'Technology', 'status' => 'active');
$result = $spdo->insert('categories', $category);
Returning data: Inserted Row Id
First parameter: table name
Secondary paramater: column names and values
$category = array('name' => 'Technology', 'status' => 'active');
$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.
$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
$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.
$result = $spdo->delete('categories', array('ID = 5'));
First paramater: table name
Secondary paramater: where block
$result = $spdo->delete('categories', array('ID = ?'), array(5));
First paramater: table name
Secondary parameter: where block
Third parameter: where block values
$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.
Samples are in the below:
Sample 1:
$result = $spdo->execute('INSERT INTO categories SET name = ?, status = ?', array('bindValues' => array('Technology', 'status')));
Sample 2:
$result = $spdo->execute('INSERT INTO categories SET name = ?, status = ?', array('bindValues' => array('Technology', 'active'), 'configKey' => 'secondaryDB'));
Sample 3:
$result = $spdo->execute('DELETE FROM categories WHERE ID = ?', array('bindValues' => array(5)));
Sample 4:
$result = $spdo->execute('DELETE FROM categories WHERE ID = ?', array('bindValues' => array(5), 'configKey' => 'secondaryDB'));
Sample 5:
$result = $spdo->execute('UPDATE categories SET name = ? WHERE ID = ?', array('bindValues' => array('Tech', 5)));
Sample 6
$result = $spdo->execute('UPDATE categories SET name = ? WHERE ID = ?', array('bindValues' => array('Tech', 5), 'configKey' => 'secondaryDB'));
$spdo->errors();
This function shows all errors.
$errors = $spdo->getErrors();
This function returns all errors.
$spdo->getLastQuery();
This function will return data as following;
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