Posted By

FrankBr on 03/12/09


Tagged

sql dump pdo


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

dantreacy


PDO SQL DUMP


 / Published in: PHP
 

A small PDO Dump Script ( for mysql 2.11.7 and perhaps other versions )

  1. $sDatabase = '';
  2. $sDsn = '';
  3. $sUser = '';
  4. $sPass = '';
  5.  
  6. $PDO = new PDO( $sDsn, $sUser, $sPass );
  7. $PDO->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
  8.  
  9. $sQuery = "SHOW tables FROM " . $sDatabase;
  10. $sResult = $PDO->query($sQuery);
  11. $sData = "
  12. -- PDO SQL Dump --
  13.  
  14. SET SQL_MODE=\"NO_AUTO_VALUE_ON_ZERO\";
  15.  
  16. --
  17. -- Database: `$sDatabase`
  18. --
  19.  
  20. -- --------------------------------------------------------
  21. ";
  22.  
  23. while ($aTable = $sResult->fetch(PDO::FETCH_ASSOC)) {
  24.  
  25. $sTable = $aTable['Tables_in_' . $sDatabase];
  26.  
  27. $sQuery = "SHOW CREATE TABLE $sTable";
  28.  
  29. $sResult2 = $PDO->query($sQuery);
  30.  
  31. $aTableInfo = $sResult2->fetch(PDO::FETCH_ASSOC);
  32.  
  33. $sData .= "\n\n--
  34. -- Tabel structuur voor tabel `$sTable`
  35. --\n\n";
  36. $sData .= $aTableInfo['Create Table'] . ";\n";
  37.  
  38. $sData .= "\n\n--
  39. -- Gegevens worden uitgevoerd voor tabel `$sTable`
  40. --\n\n";
  41.  
  42.  
  43. $sQuery = "SELECT * FROM $sTable\n";
  44.  
  45. $sResult3 = $PDO->query($sQuery);
  46.  
  47. while ($aRecord = $sResult3->fetch(PDO::FETCH_ASSOC)) {
  48.  
  49. // Insert query per record
  50. $sData .= "INSERT INTO $sTable VALUES (";
  51. $sRecord = "";
  52. foreach( $aRecord as $sField => $sValue ) {
  53. $sRecord .= "'$sValue',";
  54. }
  55. $sData .= substr( $sRecord, 0, -1 );
  56. $sData .= ");\n";
  57. }
  58.  
  59. }

Report this snippet  

You need to login to post a comment.