Advertisement
Guest User

Untitled

a guest
Mar 30th, 2016
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 KB | None | 0 0
  1. <?php
  2. error_reporting(E_ALL);
  3. ini_set('display_errors', '1');
  4.  
  5.  
  6. $mysql_host = 'localhost';
  7. $mysql_username = '';
  8. $mysql_password = '';
  9. $mysql_database = '';
  10.  
  11. $db = new PDO('mysql:dbname='.$mysql_database.';host='.$mysql_host,$mysql_username,$mysql_password);
  12.  
  13. // works not with the following set to 0. You can comment this line as 1 is default
  14. $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, 1);
  15.  
  16. function truncate_db()
  17. {
  18. global $db;
  19.  
  20. $sql_query_1 = "
  21. TRUNCATE TABLE `WIZYTY`;
  22. TRUNCATE TABLE `ANIMALS`;
  23. TRUNCATE TABLE `DOCTORS`;
  24. TRUNCATE TABLE `CUSTOMER`
  25. ";
  26.  
  27. try {
  28. $stmt = $db->prepare($sql_query_1);
  29. $stmt->execute();
  30. echo "Truncate action - OK";
  31. }
  32. catch (PDOException $e)
  33. {
  34. echo $e->getMessage();
  35. die();
  36. }
  37. }
  38.  
  39. function import_db()
  40. {
  41. global $db;
  42.  
  43. try
  44. {
  45. $sql_query_2 = implode(array_map(function ($v) {
  46. return file_get_contents($v);
  47. }, glob(__DIR__ . "/*.sql")));
  48.  
  49. $qr = $db->exec($sql_query_2);
  50. echo "Import action - OK";
  51. }
  52. catch (PDOException $e)
  53. {
  54. echo 'Connection failed: ' . $e->getMessage();
  55. }
  56. }
  57.  
  58. truncate_db();
  59. echo '<br />';
  60. import_db();
  61.  
  62. $db = null;
  63. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement