Taka pro¶ciutka klasa mo¿e siê komu¶ przyda. Co s±dzicie o tej klasie, czy jest dobrze zrobiona czy jako¶ ulepszyæ !
include_once http://www.php.net/basename('databaseClass.php'); /** * PHP 5.1 */ /** * createtableclass * Class is responsible for adding and removing, * tables and queries mysql opomocą PDO library. * Klasa odpowiada za dodawanie * i usuwanie tabel i zapytań mysql za opomocą biblioteki PDO. * @version 1.00 * @license free, Noncommercial — You may not use this work for commercial purposes. * @author Adam Berger <ber34@o2.pl> * @Site www.joomla-cms.com.pl */ class createtableclass{ private $db; public $error = false; public function __construct(){ $this->db = new databaseClass(); // połączenie z bazą z autoload $this->error; } public function error_table(){ if($this->error !== false){ return $this->error; } } public function drop_table($table){ if(!http://www.php.net/empty($table)){ $sql4 ='DROP TABLE IF EXISTS '.$table; // szukamy tabeli if($this->db->exec($sql4) !== false){ $this->error= 'Usunięto Tabelę.'; } else { $this->error= 'Nie Usunięto Tabeli .'.$table; } }else{$this->error="Podaj nazwę tabeli do usunięcia";} } public function create_table($table, $sql2, $insert=null, $insert_execute=null){ try { if(!http://www.php.net/empty($table) && !http://www.php.net/empty($sql2)){ /* Sprawdzamy niezależnie od podania parametru IF NOT EXISTS ponieważ dostaniemy odpowiedź i wyświetlimy ją sobie na ekranie */ $sql ='SHOW TABLES LIKE :table'; // szukamy tabeli $stmt = $this->db->prepare($sql); $stmt->bindValue(':table', $table, PDO::PARAM_STR); $stmt->execute(); if($stmt->fetch() > 0){ $this->error ='Tabela istnieje.'; }else{ if($this->db->exec($sql2) !== false){ $this->error= 'Tabela utworzona pomyślnie.'; // return 1; } else { $this->error= 'Nie utworzona tabeli .'.$table; // return 0; } // po utworzeniu tabeli możemy przypisać insert if(!http://www.php.net/empty($insert) && !http://www.php.net/empty($insert_execute)){ $q = $this->db->prepare($insert); $q->execute($insert_execute); } } }else{ $this->error="Podaj nazwę tabeli lub zapytanie"; } }catch(PDOException $e){ http://www.php.net/echo 'Połączenie nie mogło zostać utw.<br />'.$e->getMessage(); } } }
include_once http://www.php.net/basename('create-table-class-pdo.php'); /** * PHP 5.1 */ /** * index.php * * @version 1.00 * @license free, Noncommercial — You may not use this work for commercial purposes. * @author Adam Berger <ber34@o2.pl> * @Site www.joomla-cms.com.pl */ $create = new createtableclass(); $table="admin"; $sql2 = 'CREATE TABLE IF NOT EXISTS `admin` (' .'`id_ad` int(11) NOT NULL AUTO_INCREMENT,' .'`login` varchar(255) NOT NULL,' .'`password` varchar(255) NOT NULL,' .'`sesion` varchar(255) NOT NULL,' .'PRIMARY KEY (`id_ad`)' .') ENGINE=MyISAM DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;'; ################################################################################
## // zapytanie $insert=$sql3 = "INSERT INTO `admin` (`id_ad`, `login`, `password`, `sesion`) VALUES (?, ?, ?, ?)"; // dane dla insert $insert_execute = http://www.php.net/array('2', 'admin', 'admin', '0'); $create->drop_table($table); /* or */ $create->create_table($table, $sql2, $insert, $insert_execute); http://www.php.net/echo $create->error_table();
Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)