/scripts/migration/drn/migrate_to/functions.inc.php
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
- <?php
- function mysql_table_to_csv($table,$filename,$db=null,$from=null,$where=''){
- global $newdb;
- if (!isset($db)) $db=$newdb;
- if (!isset($from)) $from="FROM $table";
- mysql_db_query($db,"SELECT $table.* INTO OUTFILE '$filename' FIELDS TERMINATED BY ',' ENCLOSED BY '\"' LINES TERMINATED BY '\n' $from $where");
- $res=mysql_db_query($db,"SHOW COLUMNS FROM $table");
- $fields=array();
- for ($c=0; $c<mysql_num_rows($res); $c++){
- $fields[]=mysql_result($res,$c,'Field');
- }
- $file_data=implode(",",$fields)."\n";
- $file_data.=file_get_contents($filename);
- file_put_contents($filename, $file_data);
- }
- function csv_file_to_assoc_array($source_file,$max_line_length=10000) {
- if (($handle = fopen("$source_file", "r")) !== FALSE) {
- $columns = fgetcsv($handle, $max_line_length, ",");
- $rows=array();
- while (($data = fgetcsv($handle, $max_line_length, ",")) !== FALSE) {
- while (count($data)<count($columns))
- array_push($data, NULL);
- $d=0;
- $row=array();
- foreach ($columns as $c){
- $row[$c]=$data[$d++];
- }
- $rows[]=$row;
- }
- fclose($handle);
- }
- return $rows;
- }
- function quote_all_array($values) {
- foreach ($values as $key=>$value)
- if (is_array($value))
- $values[$key] = quote_all_array($value);
- else
- $values[$key] = quote_all($value);
- return $values;
- }
- function quote_all($value) {
- if (is_null($value) || $value=='\N')
- return "NULL";
- $value = "'" . str_replace("'","\'",$value) . "'";
- return $value;
- }
- ?>