home *** CD-ROM | disk | FTP | other *** search
/ HTML Examples / db_operation.iso / PDO / db_operation.php
Encoding:
PHP Script  |  2016-01-01  |  3.1 KB  |  97 lines

  1. <?php
  2. // ╟ß╟╩╒╟ß π┌ π╧φ╤ ▐µ╟┌╧ ╟ß╚φ╟Σ╟╩
  3. $servername = "localhost";
  4. $username = "root";
  5. $password = "";
  6.  
  7. try {
  8.   $conn = new PDO("mysql:host=$servername", $username, $password);
  9.   // set the PDO error mode to exception
  10.   $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  11.   $sql = "CREATE DATABASE t001";
  12.   // use exec() because no results are returned
  13.   $conn->exec($sql);
  14.   echo "Database created successfully<br>";
  15. } catch(PDOException $e) {
  16.   echo $sql . "<br>" . $e->getMessage();
  17. }
  18.  
  19. // ┼Σ╘╟┴ ╠╧µß
  20. $dbname = 't001';
  21. try {
  22.   $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
  23.   // set the PDO error mode to exception
  24.   $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  25.  
  26.   // sql to create table
  27.   $sql = "CREATE TABLE  `table1` (
  28.     `id` INT( 5 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
  29.     `col1` VARCHAR( 124 ) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL ,
  30.     `col2` TEXT CHARACTER SET utf8 COLLATE utf8_bin NOT NULL
  31.     ) ENGINE = MYISAM ;";
  32.  
  33.   // use exec() because no results are returned
  34.   $conn->exec($sql);
  35.   echo "Table table1 created successfully<br>";
  36. } catch(PDOException $e) {
  37.   echo $sql . "<br>" . $e->getMessage();
  38. }
  39.  
  40. // ╟╧╬╟ß ╙╪╤φΣ πΣ ╟ß╚φ╟Σ╟╩
  41. try {
  42.   $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
  43.   // set the PDO error mode to exception
  44.   $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  45.   $sql = "INSERT INTO `table1` (`id`, `col1`, `col2`) VALUES
  46.         (NULL, 'Ahmed', 'He is a teacher.'),
  47.         (NULL, 'Ali', 'He is a doctor.');";
  48.   // use exec() because no results are returned
  49.   $conn->exec($sql);
  50.   echo "New 2 records created successfully<br>";
  51. } catch(PDOException $e) {
  52.   echo $sql . "<br>" . $e->getMessage();
  53. }
  54.  
  55. // ╟▌╤╟█ ╠╧µß
  56. try {
  57.   $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
  58.   // set the PDO error mode to exception
  59.   $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  60.   $sql = "TRUNCATE TABLE `table1`;";
  61.   // use exec() because no results are returned
  62.   $conn->exec($sql);
  63.   echo "TRUNCATE table successfully<br>";
  64. } catch(PDOException $e) {
  65.   echo $sql . "<br>" . $e->getMessage();
  66. }
  67.  
  68. // ═╨▌ ╠╧µß
  69. try {
  70.   $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
  71.   // set the PDO error mode to exception
  72.   $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  73.   $sql = "DROP TABLE `table1`;";
  74.   // use exec() because no results are returned
  75.   $conn->exec($sql);
  76.   echo "DROP TABLE successfully<br>";
  77. } catch(PDOException $e) {
  78.   echo $sql . "<br>" . $e->getMessage();
  79. }
  80.  
  81. // ═╨▌ ▐╟┌╧╔ ╚φ╟Σ╟╩
  82. try {
  83.   $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
  84.   // set the PDO error mode to exception
  85.   $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  86.   $sql = "DROP DATABASE  `t001`;";
  87.   // use exec() because no results are returned
  88.   $conn->exec($sql);
  89.   echo "DROP DATABASE successfully<br>";
  90. } catch(PDOException $e) {
  91.   echo $sql . "<br>" . $e->getMessage();
  92. }
  93.  
  94. $conn = null;
  95. ?>
  96.  
  97.