V
V
Viktor Viktorov2017-12-29 00:51:31
PHP
Viktor Viktorov, 2017-12-29 00:51:31

How to save data from MySQL database to excel using php?

Wrote a simple script:

<?php
header('Content-Type: text/html; charset=utf-8');
$host='localhost';
$database='lalala';
$user='lalala_user';
$pswd='12345';
 
$dbh = mysql_connect($host, $user, $pswd) or die("Не могу соединиться с MySQL.");
mysql_select_db($database) or die("Не могу подключиться к базе.");

$query = "SELECT * FROM `oc_product`";
$res = mysql_query($query);

while($row = mysql_fetch_array($res)){
  echo "Номер: ".$row['product_id']."<br>\n";
  echo "Модель: ".$row['model']."<br>\n";
  echo "Артикул: ".$row['sku']."<br>\n";
  echo "Цена: ".$row['price']."<br>\n";
  echo "Картинка: ".$row['image']."<br>\n";
  echo "Производитель(id): ".$row['manufacturer_id']."<br><hr>\n";
}

mysql_close($dbh);
?>

But it simply displays this data on the screen, but it needs to be saved to an Excel table, preferably in the .xls format, or at least .csv.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
R
Rsa97, 2017-12-29
@Rsa97

https://habrahabr.ru/post/245233/

J
Justique, 2017-12-29
@Justique

fputcsv

X
xmoonlight, 2018-01-24
@xmoonlight

https://phpspreadsheet.readthedocs.io/en/develop/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question