PageRenderTime 42ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/scripts/migration/drn/migrate_to/functions.inc.php

https://bitbucket.org/drn05r/neurohub-dev-davidn
PHP | 54 lines | 49 code | 5 blank | 0 comment | 9 complexity | ef0a632bb285f85c873c61dec79f2a31 MD5 | raw file
Possible License(s): GPL-2.0, AGPL-1.0, BSD-3-Clause, LGPL-2.1, MPL-2.0-no-copyleft-exception
  1. <?php
  2. function mysql_table_to_csv($table,$filename,$db=null,$from=null,$where=''){
  3. global $newdb;
  4. if (!isset($db)) $db=$newdb;
  5. if (!isset($from)) $from="FROM $table";
  6. mysql_db_query($db,"SELECT $table.* INTO OUTFILE '$filename' FIELDS TERMINATED BY ',' ENCLOSED BY '\"' LINES TERMINATED BY '\n' $from $where");
  7. $res=mysql_db_query($db,"SHOW COLUMNS FROM $table");
  8. $fields=array();
  9. for ($c=0; $c<mysql_num_rows($res); $c++){
  10. $fields[]=mysql_result($res,$c,'Field');
  11. }
  12. $file_data=implode(",",$fields)."\n";
  13. $file_data.=file_get_contents($filename);
  14. file_put_contents($filename, $file_data);
  15. }
  16. function csv_file_to_assoc_array($source_file,$max_line_length=10000) {
  17. if (($handle = fopen("$source_file", "r")) !== FALSE) {
  18. $columns = fgetcsv($handle, $max_line_length, ",");
  19. $rows=array();
  20. while (($data = fgetcsv($handle, $max_line_length, ",")) !== FALSE) {
  21. while (count($data)<count($columns))
  22. array_push($data, NULL);
  23. $d=0;
  24. $row=array();
  25. foreach ($columns as $c){
  26. $row[$c]=$data[$d++];
  27. }
  28. $rows[]=$row;
  29. }
  30. fclose($handle);
  31. }
  32. return $rows;
  33. }
  34. function quote_all_array($values) {
  35. foreach ($values as $key=>$value)
  36. if (is_array($value))
  37. $values[$key] = quote_all_array($value);
  38. else
  39. $values[$key] = quote_all($value);
  40. return $values;
  41. }
  42. function quote_all($value) {
  43. if (is_null($value) || $value=='\N')
  44. return "NULL";
  45. $value = "'" . str_replace("'","\'",$value) . "'";
  46. return $value;
  47. }
  48. ?>