/database-pdo

Simple PHP database class based on PDO class. This class adds 2 new methods - for ping and bulk insert, which are useful for web scrapers. All PDO functionality is preserved.

Primary LanguagePHPMIT LicenseMIT

database-pdo

license paypal

Simple PHP database class based on PDO class. This class adds 2 new methods - for ping and bulk insert, which are useful for web scrapers. All PDO functionality is preserved.


Install

$ git clone https://github.com/oliverfindl/database-pdo.git

Usage

  • Class initialization is same as for PDO class
  • Ping method doesn't require any argument
  • Insert method has 3 arguments:
    • Table (required), into which will be data inserted
    • Array (required), which has to be array of associative arrays of data
    • Mode (optional):
      • \OliverFindl\Database::INSERT_DEFAULT:
         INSERT INTO table (...) VALUES (...), (...), ... ;
      • \OliverFindl\Database::INSERT_IGNORE:
         INSERT IGNORE INTO table (...) VALUES (...), (...), ... ;
      • \OliverFindl\Database::INSERT_UPDATE:
         INSERT INTO table (...) VALUES (...), (...), ... ON DUPLICATE KEY UPDATE column = VALUES(column), ... ;
      • \OliverFindl\Database::INSERT_REPLACE:
         REPLACE INTO table (...) VALUES (...), (...), ... ;

Example

require_once("./vendor/database-pdo/src/database.class.php");
// use \OliverFindl\Database; // optional, if you want omit namespace in code

try {
	$db = new \OliverFindl\Database(/* ... */); // same arguments as PDO
	$db->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_WARNING);
	$db->setAttribute(\PDO::ATTR_DEFAULT_FETCH_MODE, \PDO::FETCH_OBJ);
} catch(\Exception $e) {
	exit($e->getMessage());
}

// ...

try {
	$db->ping();
} catch(\Exception $e) {
	exit($e->getMessage());
}

// ...

try {
	$res = $db->insert("table", $data, \OliverFindl\Database::INSERT_UPDATE);
	if($res) $id = $db->lastInsertId();
} catch(\Exception $e) {
	exit($e->getMessage());
}

Requirements

Notes

This class is compliant with selected PDO::ERR_MODE.


License

MIT