Advertisement
Guest User

Untitled

a guest
Feb 24th, 2016
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.39 KB | None | 0 0
  1. <?php
  2.  
  3. $host = "[your database server";]
  4. $database = "[your database name]";
  5. $user = "[your user]";
  6. $pass = "[your password]";
  7.  
  8. // database connection
  9. mysql_connect($host, $user, $pass) or die ('cannot connect to the database: ' . mysql_error());
  10. mysql_select_db($database) or die ('cannot select database: ' . mysql_error());
  11.  
  12. function safe_json($json) {
  13. $json = empty($json) ? '[]' : $json ;
  14. $search = array('\\',"\n","\r","\f","\t","\b","'") ;
  15. $replace = array('\\\\',"\\n", "\\r","\\f","\\t","\\b", "&#039");
  16. $json = str_replace($search,$replace,$json);
  17. return $json;
  18. }
  19.  
  20. // function for creating default 200 OK
  21. function getOK($table)
  22. {
  23.  
  24. $R = array();
  25. $R['description'] = $table . " response";
  26. $R['schema'] = array();
  27.  
  28. $S = array();
  29. $S['type'] = "array";
  30.  
  31. $I = array();
  32. $I['$ref'] = "#/definitions/" . $table;
  33.  
  34. $S['items'] = $I;
  35.  
  36. $R['schema'] = $S;
  37.  
  38. $ok = new stdClass;
  39. $response = "200";
  40. $ok->$response = $R;
  41.  
  42. return $ok;
  43. }
  44.  
  45. $Spec_Name = str_replace("_"," ",$database);
  46. $Spec_Name = ucwords($Spec_Name);
  47.  
  48. // loop through table + fields
  49. $loop = mysql_query("SHOW tables FROM " . $database) or die ('cannot select tables');
  50.  
  51. while($table = mysql_fetch_array($loop))
  52. {
  53.  
  54. // We will need to identif
  55. $this_key = "";
  56.  
  57. // The table name
  58. $table = $table[0];
  59.  
  60. $ReturnObject = array();
  61.  
  62. if($table=='arm_groups' || $table=='authorities' || $table=='central_contacts' || $table=='central_contacts')
  63. {
  64.  
  65. $select = "SELECT * FROM `" . $table . "`";
  66.  
  67. $export = mysql_query ( $select ) or die ( "Sql error : " . mysql_error( ) );
  68.  
  69. $field_names = mysql_num_fields ( $export );
  70.  
  71. for ($i = 0; $i < $field_names; $i++)
  72. {
  73. $fields[$i] = mysql_field_name( $export , $i );
  74. }
  75.  
  76. while( $row = mysql_fetch_row( $export ) )
  77. {
  78.  
  79. $Object = array();
  80.  
  81. $col = 0;
  82. foreach( $row as $value )
  83. {
  84. $field_name = $fields[$col];
  85. $Object[$field_name] = $value;
  86. $col++;
  87. }
  88.  
  89. array_push($ReturnObject,$Object);
  90.  
  91. }
  92.  
  93. $data = stripslashes(safe_json(json_encode($ReturnObject,JSON_HEX_QUOT)));
  94.  
  95. $file_name = $table . ".json";
  96. $writefile = "files/" . $file_name;
  97. echo "writing " . $writefile . "\n";
  98. $myfile = fopen($writefile, "w") or die("Unable to open file!");
  99. fwrite($myfile, $data);
  100. fclose($myfile);
  101. }
  102. }
  103. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement