A lightweight PHP PDO database singleton wrapper class.
Docs and examples: github.com/turbopixel/PHPDO-Database
- PHP 7.2+
- MySQL/MariaDB
composer require turbopixel/phpdo-database
PHPDO::connect("database-server.com", "database_name", "user_name", "myPassword123");
PHPDO::get()->query("SELECT stars FROM github")->fetchAll();
\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.
\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()
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]);
\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);
\PHPDO\PHPDO::get()->fetchAll("SELECT id FROM github WHERE id = :repo", ["repo" => 553]);
\PHPDO\PHPDO::get()->rowCount("SELECT id FROM github WHERE id = :repo", ["repo" => 553]);
\PHPDO\PHPDO::get()->isTable("user_settings")
\PHPDO\PHPDO::get()->findPrimaryIndexColumn("tablename", "databasename")
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