Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> klasa Create Table PDO
ber32
post 15.04.2014, 14:49:54
Post #1





Grupa: Zarejestrowani
Postów: 332
Pomógł: 22
Dołączył: 6.07.2010

Ostrzeżenie: (0%)
-----


Taka prościutka klasa może się komuś przyda. Co sądzicie o tej klasie, czy jest dobrze zrobiona czy jakoś ulepszyć !

  1. include_once basename('databaseClass.php');
  2. /**
  3.  * PHP 5.1
  4.  */
  5.  
  6. /**
  7.  * createtableclass
  8.  * Class is responsible for adding and removing,
  9.  * tables and queries mysql opomocÄ… PDO library.
  10.  * Klasa odpowiada za dodawanie
  11.  * i usuwanie tabel i zapytaĹ„ mysql za opomocÄ… biblioteki PDO.
  12.  * @version 1.00
  13.  * @license free, Noncommercial — You may not use this work for commercial purposes.
  14.  * @author Adam Berger <ber34@o2.pl>
  15.  * @Site www.joomla-cms.com.pl
  16.  */
  17.  
  18. class createtableclass{
  19.  
  20. private $db;
  21. public $error = false;
  22.  
  23. public function __construct(){
  24. $this->db = new databaseClass(); // połączenie z bazą z autoload
  25. $this->error;
  26. }
  27.  
  28. public function error_table(){
  29. if($this->error !== false){
  30. return $this->error;
  31. }
  32. }
  33.  
  34. public function drop_table($table){
  35. if(!empty($table)){
  36. $sql4 ='DROP TABLE IF EXISTS '.$table; // szukamy tabeli
  37. if($this->db->exec($sql4) !== false){
  38. $this->error= 'Usunięto Tabelę.';
  39. } else {
  40. $this->error= 'Nie Usunięto Tabeli .'.$table;
  41. }
  42. }else{$this->error="Podaj nazwę tabeli do usunięcia";}
  43. }
  44.  
  45. public function create_table($table, $sql2, $insert=null, $insert_execute=null){
  46. try {
  47. if(!empty($table) && !empty($sql2)){
  48. /* Sprawdzamy niezaleĹźnie od podania parametru IF NOT EXISTS
  49.   poniewaĹź dostaniemy odpowiedĹş i wyĹ›wietlimy jÄ… sobie na ekranie */
  50. $sql ='SHOW TABLES LIKE :table'; // szukamy tabeli
  51. $stmt = $this->db->prepare($sql);
  52. $stmt->bindValue(':table', $table, PDO::PARAM_STR);
  53. $stmt->execute();
  54. if($stmt->fetch() > 0){
  55. $this->error ='Tabela istnieje.';
  56. }else{
  57.  
  58. if($this->db->exec($sql2) !== false){
  59. $this->error= 'Tabela utworzona pomyślnie.';
  60. // return 1;
  61. } else {
  62. $this->error= 'Nie utworzona tabeli .'.$table;
  63. // return 0;
  64. }
  65. // po utworzeniu tabeli moşemy przypisać insert
  66. if(!empty($insert) && !empty($insert_execute)){
  67. $q = $this->db->prepare($insert);
  68. $q->execute($insert_execute);
  69. }
  70. }
  71. }else{
  72. $this->error="Podaj nazwÄ™ tabeli lub zapytanie";
  73. }
  74. }catch(PDOException $e){
  75. echo 'Połączenie nie mogło zostać utw.<br />'.$e->getMessage();
  76. }
  77.  
  78. }
  79. }

index.php
  1. include_once basename('create-table-class-pdo.php');
  2. /**
  3.  * PHP 5.1
  4.  */
  5.  
  6. /**
  7.  * index.php
  8.  *
  9.  * @version 1.00
  10.  * @license free, Noncommercial — You may not use this work for commercial purposes.
  11.  * @author Adam Berger <ber34@o2.pl>
  12.  * @Site www.joomla-cms.com.pl
  13.  */
  14.  
  15.  
  16. $create = new createtableclass();
  17.  
  18. $table="admin";
  19.  
  20. $sql2 = 'CREATE TABLE IF NOT EXISTS `admin` ('
  21. .'`id_ad` int(11) NOT NULL AUTO_INCREMENT,'
  22. .'`login` varchar(255) NOT NULL,'
  23. .'`password` varchar(255) NOT NULL,'
  24. .'`sesion` varchar(255) NOT NULL,'
  25. .'PRIMARY KEY (`id_ad`)'
  26. .') ENGINE=MyISAM DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;';
  27. ################################################################################
    ##
  28.  
  29. // zapytanie
  30. $insert=$sql3 = "INSERT INTO `admin` (`id_ad`, `login`, `password`, `sesion`) VALUES
  31. (?, ?, ?, ?)";
  32. // dane dla insert
  33. $insert_execute = array('2', 'admin', 'admin', '0');
  34.  
  35. $create->drop_table($table); /* or */ $create->create_table($table, $sql2, $insert, $insert_execute);
  36.  
  37.  
  38. echo $create->error_table();



--------------------
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 28.03.2024 - 17:46