Advertisement
Guest User

Untitled

a guest
Mar 2nd, 2016
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.94 KB | None | 0 0
  1. class DatabaseConnection {
  2.  
  3. protected $db_user = '';
  4. protected $db_pass = '';
  5. protected $db_dsn = 'mysql:host=localhost;dbname=';
  6. protected $options = array(
  7. PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
  8. PDO::ATTR_ERRMODE => PDO::ERRMODE_WARNING,
  9. );
  10.  
  11. public function dbconnect(){
  12. try {
  13. $db = new PDO($this->db_dsn, $this->db_user, $this->db_pass, $this->options);
  14. }
  15. catch (PDOException $e) {
  16. echo 'Connection failed: ' . $e->getMessage();
  17. exit;
  18. }
  19. return $db;
  20. }
  21. }
  22.  
  23. class dbTools {
  24.  
  25. private $db;
  26. private $verifiedTable;
  27. private $verifiedColumn;
  28. private $verifiedItemId;
  29.  
  30. function __construct() {
  31. $this->db = new DatabaseConnection();
  32. $this->db = $this->db->dbconnect();
  33. }
  34.  
  35. public function validateValues($table, $column, $itemId) {
  36. // Verify that table exists
  37. $verifiedTable = $this->verifyTable($table);
  38. $this->verifiedTable = $verifiedTable;
  39.  
  40. // Verify that column exists
  41. $verifiedColumn = $this->verifyColumn($column, $this->verifiedTable);
  42. $this->verifiedColumn = $verifiedColumn;
  43.  
  44. // Verify that id exists
  45. $verifiedItemId = $this->verifyItemId($this->verifiedTable, $itemId);
  46. $this->verifiedItemId = $verifiedItemId;
  47. }
  48.  
  49. private function verifyTable($table) {
  50. $result = $this->db->query("show tables");
  51. $final = $result->fetchAll(PDO::FETCH_COLUMN);
  52.  
  53. if(in_array($table, $final)) {
  54. $verifiedTable = $table;
  55. } else {
  56. throw new exception('Table doesn´t exists');
  57. }
  58. return $verifiedTable;
  59. }
  60.  
  61. private function verifyColumn($column, $verifiedTable) {
  62. $result = $this->db->query('SHOW COLUMNS FROM '.$verifiedTable.'');
  63. $columns = $result->fetchAll(PDO::FETCH_COLUMN);
  64.  
  65. if(in_array($column, $columns)) {
  66. $verifiedColumn = $column;
  67. } else {
  68. throw new exception('Column doesn´t exist');
  69. }
  70. return $verifiedColumn;
  71. }
  72.  
  73. private function verifyItemId($verifiedTable, $itemId) {
  74. $result = $this->db->query('SELECT id FROM '.$verifiedTable.'');
  75. $ids = $result->fetchAll(PDO::FETCH_COLUMN);
  76.  
  77. if(in_array($itemId, $ids)) {
  78. $verifiedItemId = $itemId;
  79. } else {
  80. throw new exception('Id doesn´t exist');
  81. }
  82. return $verifiedItemId;
  83. }
  84.  
  85. public function getItem() {
  86. $statement = 'SELECT '.$this->verifiedColumn.' FROM '.$this->verifiedTable.' WHERE id = :id';
  87. $query = $this->db->prepare($statement);
  88. $query->bindParam(':id', $this->verifiedItemId);
  89. $query->execute();
  90. $row = $query->fetchColumn();
  91. return $row;
  92. }
  93. }
  94.  
  95. $dbTools = new dbTools();
  96. $dbTools->validateValues('table','column', 'id');
  97. echo $dbTools->getItem();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement