Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- Upcount Database Class
- With great inspiration from Zula (tangocms.org)
- Thanks for the suggestions and ideas!
- */
- class DB extends PDO {
- public function __construct($connection_type,$database){
- $this->database_init($connection_type,$database,$connection_information=array());
- }
- public function database_init($connection_type,$database,$pdo_options=array()){
- $user = "";
- $pass = "";
- // Correct any possible issues that may have occured.
- $connection_type = strval($connection_type);
- // Check to make sure database driver is available
- if(in_array( $connection_type,PDO::getAvailableDrivers())){
- // Now we check to see what type of driver is usable now
- switch($connection_type){
- case 'mysql':
- break;
- // Our default and only properly handled option is SQLite (sorry if you wanted mysql)
- case 'sqlite':
- default:
- $database_driver = 'sqlite';
- break;
- }
- } else {
- print "Driver Failure";
- //throw new DatabaseDriverFailure("Databse does not exist");
- // Error Occured {Error handler should probably impliment}
- } // End the Checks
- // Attempt to connect
- try {
- $connector = $database_driver.":";
- if(!is_array( $pdo_options )){
- $pdo_options = array(
- PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
- );
- }
- if(file_exists($database)){
- $connector .= $database;
- } else {
- print "File Failure";
- }
- parent::__construct($connector,$user,$pass,$pdo_options);
- parent::setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- } catch ( PDOexception $e ){
- print "PDO Failure $e";
- }
- } // end method init
- public function check_valid($name){
- if(preg_match('#[A-Z][A-Z0-9_\-]+#i',$name)){
- return true;
- } else {
- return false;
- }
- }
- public function table_exists($table){
- $query = "SELECT * from sqlite_master where type='table' and name=:table;";
- if($check = parent::prepare($query)) { print "yes"; } else { print "no"; }
- if( $this->check_valid($table) ) {
- $check->execute(array(":table" => $table));
- var_dump($check);
- }
- }
- }
- echo "\n";
- $db = new DB('sqlite',"upcount.db");
- $database = $db->query("SELECT * FROM users WHERE ID=1;")->fetchAll();
- var_dump($database);
- $db->table_exists('users');
- ?>
Add Comment
Please, Sign In to add comment