Advertisement
Guest User

Untitled

a guest
Feb 24th, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.17 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 for creating default 200 OK
  13. function getOK($table)
  14. {
  15.  
  16. $R = array();
  17. $R['description'] = $table . " response";
  18. $R['schema'] = array();
  19.  
  20. $S = array();
  21. $S['type'] = "array";
  22.  
  23. $I = array();
  24. $I['$ref'] = "#/definitions/" . $table;
  25.  
  26. $S['items'] = $I;
  27.  
  28. $R['schema'] = $S;
  29.  
  30. $ok = new stdClass;
  31. $response = "200";
  32. $ok->$response = $R;
  33.  
  34. return $ok;
  35. }
  36.  
  37. $Spec_Name = str_replace("_"," ",$database);
  38. $Spec_Name = ucwords($Spec_Name);
  39.  
  40. // loop through table + fields
  41. $loop = mysql_query("SHOW tables FROM " . $database) or die ('cannot select tables');
  42.  
  43. while($table = mysql_fetch_array($loop))
  44. {
  45.  
  46. // We will need to identif
  47. $this_key = "";
  48.  
  49. // The table name
  50. $table = $table[0];
  51.  
  52. $header = "";
  53. $data = "";
  54.  
  55. $select = "SELECT * FROM `" . $table . "`";
  56.  
  57. $export = mysql_query ( $select ) or die ( "Sql error : " . mysql_error( ) );
  58.  
  59. $fields = mysql_num_fields ( $export );
  60.  
  61. for ( $i = 0; $i < $fields; $i++ )
  62. {
  63. $header .= mysql_field_name( $export , $i ) . "\t";
  64. }
  65.  
  66. while( $row = mysql_fetch_row( $export ) )
  67. {
  68. $line = '';
  69. foreach( $row as $value )
  70. {
  71. if ( ( !isset( $value ) ) || ( $value == "" ) )
  72. {
  73. $value = "\t";
  74. }
  75. else
  76. {
  77. $value = str_replace( '"' , '""' , $value );
  78. $value = '"' . $value . '"' . "\t";
  79. }
  80. $line .= $value;
  81. }
  82. $data .= trim( $line ) . "\n";
  83. }
  84. $data = str_replace( "\r" , "" , $data );
  85.  
  86. if ( $data == "" )
  87. {
  88. $data = "\n(0) Records Found!\n";
  89. }
  90.  
  91. if($table!='clinical_study')
  92. {
  93. $file_name = $table . ".json";
  94. $writefile = "files/" . $file_name;
  95. echo "writing " . $writefile . "<br />";
  96. $myfile = fopen($writefile, "w") or die("Unable to open file!");
  97. fwrite($myfile, $data);
  98. fclose($myfile);
  99. }
  100. }
  101. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement