php - Getting data from mysql and creating a csv file issue -
php - Getting data from mysql and creating a csv file issue -
i want extract info , create csv file. how attributes table looks like. can see in picture: here
this code, tried:
$select = mysql_query("select * attributes"); $final =""; $i = 0; while($row = mysql_fetch_array($select)) { $id_produs = $row['id_produs']; $n_attr = $row['nume_attr']; $val = $row['val_attr']; $final .= $id_produs . "%%" . $val . "%%"; $csv_array_attributes[$i] = $final; $i++; }
this part when create csv file:
$file = fopen("attributes.csv", "w+"); foreach ($csv_array_attributes $line) { fputcsv($file, explode('%%', $line), "^", "`"); } fclose($file);
and wanted result: here. so, nume_attr
should header every column in csv , val_attr
should values every nume_attr
(see image).
i tried many ways don't result image. have mention table "attributes" has more 74000 rows.
this table structure:
create table `attributes` ( `id` int(11) not null auto_increment, `id_produs` text not null, `nume_attr` text not null, `val_attr` text not null, primary key (`id`) ) engine=innodb auto_increment=673912 default charset=latin1;
/*try next code */ $select = mysql_query("select * attributes"); $i = 0; $csv_array_attributes[$i]=array('sku','manufacturer_article','manufacturer'); $i++; while($row = mysql_fetch_array($select)) { $csv_array_attributes[$i] =array('sku'=>$row['id_produs'],'manufacturer_article'=>$row['nume_attr'],'manufacturer'=>$row['val_attr']); $i++; } $file = fopen("attributes.csv", "w+"); foreach ($csv_array_attributes $line) { fputcsv($file,$line); } fclose($file);
php sql csv
Comments
Post a Comment