2016-07-06 6 views
0

J'ai un script ci-dessous pour exporter les données MySQL vers un fichier csv. Ce script exporte tout dans la boucle while mais je veux mettre une valeur constante dans une 2ème colonne ou je veux juste garder la 2ème colonne vide et mettre des données MySQL dans la colonne 1 et colonne 3.Exporter MySQL vers CSV

// output the column headings 
fputcsv($output, array('Column 1', 'Column 2', 'Column 3')); 

// fetch the data 
$rows = mysql_query('SELECT field1,field2 FROM table'); 

// loop over the rows, outputting them 
while ($row = mysql_fetch_assoc($rows)) fputcsv($output, $row); 
+0

'(sélectionner 'Colonne 1', 'Colonne 2', 'Colonne 3') union (SELECT champ1, "", field2 à partir de la table) en 'filename' OUTFILE FIELDS TERMINATED BY '' ENCLOSED BY '"' LIGNES TERMINÉES PAR '\ n';' – splash58

Répondre

1

Vous avez déjà field1 (c.-à-Column1) et field2 (c.-à-Colonne3)
donc il suffit d'ajouter Null à la 2ème position comme ci-dessous

$insert = array(''); 
array_splice($row, 1, 0, $insert); 

puis utilisez

fputcsv($output, $row);