neil_pearce

DROP TABLES

Jan 22nd, 2023
1,115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.92 KB | Source Code | 0 0
  1. <?php
  2. declare(strict_types=1);
  3.  
  4. /*
  5.  * Configuration
  6.  */
  7. $dbname = '';
  8. $username = '';
  9. $password = '';
  10.  
  11. /* list of tables to drop */
  12. $tables = ['table1', 'table2', 'table3'];
  13.  
  14. $eol = empty($argc) ? '<br>' : PHP_EOL;  // determine end of line
  15.  
  16.  
  17. /*
  18.  * Connect to database and attempt to drop tables
  19.  */
  20. $dbc = new PDO("mysql:host=localhost;dbname=$dbname", $username, $password);
  21.  
  22. foreach ($tables as $table) {
  23.     try {
  24.         /* format DROP statement */
  25.         $sql = 'DROP TABLE ' . $table;
  26.         echo $eol, $sql, $eol;
  27.  
  28.         /* execute statement */
  29.         $dbc->exec($sql);
  30.  
  31.         /* SQLSTATE should be '00000' */
  32.         $info = $dbc->errorInfo();
  33.         if ($info[0] === PDO::ERR_NONE) {
  34.             $info[2] = 'Command successful';
  35.         }
  36.  
  37.         echo sprintf('SQLSTATE[%s]: %s', $info[0], $info[2]), $eol;
  38.     } catch (PDOException $e) {
  39.         echo $e->getMessage(), $eol;
  40.     }
  41. }
  42.  
Advertisement
Add Comment
Please, Sign In to add comment