簡體   English   中英

php腳本將mysql表數據導出到csv,從某些列轉義數據並將字符串映射到數組?

[英]Php script to export mysql table data to a csv, escaping data from certain columns and mapping strings to arrays?

我需要將舊的錯誤跟蹤器(Zentrack)中的數據遷移到新的錯誤跟蹤器中,唯一可以使用的導入方法是從CSV導入。 為此,我需要來自兩個表,票證和日志的數據,因此我在php中編寫了以下腳本。

<?php

error_reporting(E_ALL);

$host = "localhost"; 
$user = "dbuser"; 
$pass = "dbpass"; 
$db = "zentrk";

$users[1] = 'john';
$users[4] = 'sally';
$users[5] = 'nick';
$users[6] = 'ralph';

$r = mysql_connect($host, $user, $pass);

if (!$r) {
    echo "Could not connect to server\n";
    trigger_error(mysql_error(), E_USER_ERROR);
}

echo mysql_get_server_info() . "\n"; 

$r2 = mysql_select_db($db);

if (!$r2) {
    echo "Cannot select database\n";
    trigger_error(mysql_error(), E_USER_ERROR); 
}

$query_tickets = "select ZENTRACK_TICKETS.id, ZENTRACK_TICKETS.title, ZENTRACK_TICKETS.priority, ZENTRACK_TICKETS.status, ZENTRACK_TICKETS.description, ZENTRACK_TICKETS.otime, 
ZENTRACK_TICKETS.type_id, ZENTRACK_TICKETS.user_id, ZENTRACK_TICKETS.system_id, ZENTRACK_TICKETS.creator_id, ZENTRACK_TICKETS.proj_key
from ZENTRACK_TICKETS
where ZENTRACK_TICKETS.status = 'OPEN' 
and ZENTRACK_TICKETS.system_id in ('18', '3', '6', '1', '16', '7', '9', '4', '20') 
and ZENTRACK_TICKETS.type_id not in ('1', '10', '5')";

$rs = mysql_query($query_tickets);

if (!$rs) {
    echo "Could not execute query: $query";
    trigger_error(mysql_error(), E_USER_ERROR); 
} else {
    echo "Query: $query executed\n";
} 

$export = array();

$export[] = 'id,title,created date,priority,type,assigned,description,system,creator,project key,log1,log2,log3,log4,log5,log6,log7,log8,log9,log10
';
while ($row = mysql_fetch_assoc($rs)) {
   $line = '';
   $count = 0;
    $line .= $row['id'] . "," . $row['title'] . "," . date('d-M-y h:m a',$row['otime']) . "," . $row['priority'] . "," . $row['type_id'] . "," . $row['user_id'] . "," . $row['description'] . "," . $row['system_id'] . "," . $row['creator_id'] . "," . $row['proj_key'] . ",";      


    $logs = find_logs($id = $row['id']);
    foreach($logs as $log_entry) {
      $line .= $log_entry.",";
      $count++;
    }
    while($count < 10) {
      $line .= ",";
      $count++;
    } 

    $export[] = $line.'
';
}

mysql_close();


// print_r($export);

$file = 'tickets.csv';

file_put_contents($file, $export);

function find_logs($ticket) {

 $content = array();

 $query = "select ZENTRACK_LOGS.created, ZENTRACK_LOGS.user_id, ZENTRACK_LOGS.entry 
          from ZENTRACK_LOGS 
          where ZENTRACK_LOGS.ticket_id = $ticket 
          and ZENTRACK_LOGS.action <> 'EDIT' ";

   $rs = mysql_query($query);

   if (!$rs) {
    echo "Could not execute query: $query";
    trigger_error(mysql_error(), E_USER_ERROR); 
    } 

   while ($row = mysql_fetch_assoc($rs)) {
    $date = date('d-M-y h:m a',$row['created']);
    $content[] = $date . ";" . $row['user_id'] . ";" . $row['entry'];
   }

 return $content;
}
?>

我在使用此腳本時遇到了兩個問題,我確定這是由於我是PHP新手。

1)我需要轉義$ row ['description']中的數據,因為它同時包含回車符和,在文本中,當保存為CSV時,錯誤地將輸出分成了新的行。 我需要將該行的內容保存在“”中,但是我不確定如何保存。

2)在find_logs函數內的$ row ['user_id'],$ row ['creator_id']和$ row ['user_id']中返回的數據返回一個數字,我需要找到該數字並替換為相應的字符串在$ users數組中。 最好的方法是什么?

處理csv文件時,請使用fgetcsvfputcsv ,為它提供一個數組,它將為您處理轉義。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM