Guest User

Untitled

a guest
May 31st, 2018
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.21 KB | None | 0 0
  1. <?php
  2. /*
  3. Upcount Database Class
  4. With great inspiration from Zula (tangocms.org)
  5. Thanks for the suggestions and ideas!
  6. */
  7.  
  8.  
  9. class DB extends PDO {
  10.  
  11. public function __construct($connection_type,$database){
  12. $this->database_init($connection_type,$database,$connection_information=array());
  13. }
  14.  
  15. public function database_init($connection_type,$database,$pdo_options=array()){
  16. $user = "";
  17. $pass = "";
  18. // Correct any possible issues that may have occured.
  19. $connection_type = strval($connection_type);
  20. // Check to make sure database driver is available
  21. if(in_array( $connection_type,PDO::getAvailableDrivers())){
  22. // Now we check to see what type of driver is usable now
  23. switch($connection_type){
  24. case 'mysql':
  25. break;
  26. // Our default and only properly handled option is SQLite (sorry if you wanted mysql)
  27. case 'sqlite':
  28. default:
  29. $database_driver = 'sqlite';
  30. break;
  31. }
  32. } else {
  33. print "Driver Failure";
  34. //throw new DatabaseDriverFailure("Databse does not exist");
  35. // Error Occured {Error handler should probably impliment}
  36. } // End the Checks
  37. // Attempt to connect
  38. try {
  39. $connector = $database_driver.":";
  40. if(!is_array( $pdo_options )){
  41. $pdo_options = array(
  42. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
  43. );
  44. }
  45. if(file_exists($database)){
  46. $connector .= $database;
  47. } else {
  48. print "File Failure";
  49. }
  50. parent::__construct($connector,$user,$pass,$pdo_options);
  51. parent::setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  52. } catch ( PDOexception $e ){
  53. print "PDO Failure $e";
  54. }
  55. } // end method init
  56.  
  57. public function check_valid($name){
  58. if(preg_match('#[A-Z][A-Z0-9_\-]+#i',$name)){
  59. return true;
  60. } else {
  61. return false;
  62. }
  63. }
  64. public function table_exists($table){
  65. $query = "SELECT * from sqlite_master where type='table' and name=:table;";
  66. if($check = parent::prepare($query)) { print "yes"; } else { print "no"; }
  67. if( $this->check_valid($table) ) {
  68. $check->execute(array(":table" => $table));
  69. var_dump($check);
  70. }
  71. }
  72.  
  73. }
  74. echo "\n";
  75. $db = new DB('sqlite',"upcount.db");
  76. $database = $db->query("SELECT * FROM users WHERE ID=1;")->fetchAll();
  77. var_dump($database);
  78. $db->table_exists('users');
  79. ?>
Add Comment
Please, Sign In to add comment