Guest User

Untitled

a guest
Dec 4th, 2018
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. <?php
  2.  
  3. $queries = [];
  4. while (true) {
  5. $query = fgets(STDIN);
  6. if (empty($query)) {
  7. break;
  8. }
  9. $queries []= $query;
  10. }
  11.  
  12. $dsn = 'mysql:host=127.0.0.1;port=3306;dbname=database1';
  13. $user = 'user';
  14. $password = 'password';
  15.  
  16. $dbh = new PDO($dsn, $user, $password);
  17.  
  18. foreach($queries as $query) {
  19. if (empty($query)) {
  20. continue;
  21. }
  22. $stmt = $dbh->prepare($query);
  23. $stmt->setFetchMode(PDO::FETCH_NUM);
  24. $stmt->execute();
  25.  
  26. $cols = $stmt->columnCount();
  27.  
  28. $tables = [];
  29. $columns = [];
  30. for ($i = 0;$i < $cols;$i++) {
  31. $col = $stmt->getColumnMeta($i);
  32. $table = $col["table"];
  33. if (!in_array($table , $tables)) {
  34. $tables []= $table;
  35. }
  36. $columns []= $col["name"];
  37. }
  38. $result = $stmt->fetchAll();
  39.  
  40. echo "\xEF\xBB\xBF";
  41. echo join("|", $tables )."\r\n";
  42. echo ",".join(",", $columns )."\r\n";
  43.  
  44. foreach($result as $row) {
  45. echo ",\"".join("\",\"", $row)."\"\r\n";
  46. }
  47. echo "\r\n";
  48. }
Add Comment
Please, Sign In to add comment