turbopixel / phpdo-database
A lightweight PHP7 database wrapper class.
Installs: 105
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 1
Forks: 1
Open Issues: 0
Type:framework
Requires
- php: >=7.2
- ext-pdo: *
Suggests
- ext-pdo_mysql: MySQL database
README
A lightweight PHP PDO database singleton wrapper class.
Docs and examples: github.com/turbopixel/PHPDO-Database
Requirements
- PHP 7.2+
- MySQL/MariaDB
Install via composer
composer require turbopixel/phpdo-database
Example
PHPDO::connect("database-server.com", "database_name", "user_name", "myPassword123"); PHPDO::get()->query("SELECT stars FROM github")->fetchAll();
class PHPDO
Create database connection
\PHPDO\PHPDO::connect("database-server.com", "database_name", "user_name", "myPassword123");
An existing PDO object can be set instead
// $pdoObject = new PDO(); PHPDO::setPdo($pdoObject);
After this, you can use the PHPDO class from everywhere.
Get instance
\PHPDO\PHPDO::get() returns the PHPDO instance
\PHPDO\PHPDO::get()
Example: Select rows
\PHPDO\PHPDO::get()->query("SELECT * FROM github")->fetchAll();
Get PDO instance
\PHPDO\PHPDO::get()->getPdo()
Run MySQL query
query
\PHPDO\PHPDO::get()->query("SELECT id FROM user WHERE active = 1"); print_r( $pdoStmnt->fetch() );
execute
\PHPDO\PHPDO::get()->execute("UPDATE user SET active = 0 WHERE mail IS NULL");
Prepared Statement
\PHPDO\PHPDO::get()->prepare("UPDATE github SET stars = stars+1 WHERE id = :id", ["id" => 1234]);
Helper
fetch() - Select a single row
\PHPDO\PHPDO::get()->fetch("SELECT id FROM github WHERE id = :repo", ["repo" => 553]);
\PHPDO\PHPDO::get()->fetch()
is a helper method and replace this:
$rows = []; $stmnt = \PHPDO\PHPDO::get()->prepare("SELECT * FROM github WHERE id = ?", [ 1234 ]); if($stmnt instanceof PDOStatement){ $rows = $stmnt->fetchAll(); }else{ die("QUERY ERROR"); } print_r($rows);
fetchAll() - Select multiple rows
\PHPDO\PHPDO::get()->fetchAll("SELECT id FROM github WHERE id = :repo", ["repo" => 553]);
rowCount() - Count rows
\PHPDO\PHPDO::get()->rowCount("SELECT id FROM github WHERE id = :repo", ["repo" => 553]);
isTable() - Check table exists (MySQL only)
\PHPDO\PHPDO::get()->isTable("user_settings")
findPrimaryIndexColumn() - Find table index column (MySQL only)
\PHPDO\PHPDO::get()->findPrimaryIndexColumn("tablename", "databasename")
Internal class logging
All SQL Queries stored in PHPDO::$logs (array). Attribute \PHPDO\PHPDO::$logging
must be true
Enable logging
\PHPDO\PHPDO::$logging = true;
Get internal query logs
Get query logs.
\PHPDO\PHPDO::get()->getLog(); // returns an array