Mysql
 sql >> Teknologi Basis Data >  >> RDS >> Mysql

Mengunduh data ke file csv dari MySQL menggunakan PHP

Jika file Anda memang ada di dalam database Anda, Anda dapat mengambilnya dengan cara ini:

Main.php:

<?php
  $host = "hostname";
  $user = "username";
  $password = "password";
  $database = "database";

  $link = mysqli_connect($host, $user, $password, $database);
  If (!$link){
      echo ("Unable to connect to database!");
  }
  else {
  $query = "SELECT * FROM participants";    
  $result = mysqli_query($link,$query);

  while ($row = mysqli_fetch_array($result, MYSQLI_BOTH)){

echo $row['name'] . " " . $row['area_of_exp'] . " " . '<a href="download.php?file='.$row['id_participants']. '"\>'." cv </a><br />";
  }


}
mysqli_close($link);
?>

mysql &versi yang lebih spesifik:

<?PHP
      include('databaseConnection.php');


      $query = "SELECT * FROM participants";    
      $result = mysql_query($query);

      while ($row = mysql_fetch_array($result, MYSQL_BOTH)){

    echo $row['name'] . " " . $row['area_of_exp'] . " " . '<a href="download.php?file='.$row['id_participants']. '"\>'." cv </a><br />";
      }

      mysql_close($link);
    ?>

download.php:

<?php


$id_participants = $_GET['file'];

$host = "hostname";
$user = "username";
$password = "password";
$database = "database";

$link = mysqli_connect($host, $user, $password, $database);
If (!$link){
      echo ("Unable to connect to database!");
  }
  else {
$query = "SELECT * FROM participants WHERE id_participants = $id_participants";

$result = mysqli_query($link, $query);
$row = mysqli_fetch_array($result, MYSQLI_BOTH);
$size = $row['File_Size'];
$type = $row['File_Type'];
$name =$row['File_Name'];
$CV = $row['cv'];
// echo $size . "". $type . " ". $name;

header("Content-length: $size");
header("Content-type: $type");
header("Content-Disposition: attachment; filename=$name");
echo $CV;

}
mysqli_close($link);

?>

Mysql &lebih spesifik:

<?php

include('databaseConnection.php');

$id_participants = $_GET['file'];


$query = "SELECT * FROM participants WHERE id_participants = $id_participants";

$result = mysql_query($query);
$row = mysql_fetch_array($result, MYSQL_BOTH);
$size = $row['File_Size'];
$type = $row['File_Type'];
$name =$row['File_Name'];
$CV = $row['cv'];
// echo $size . "". $type . " ". $name;

header("Content-length: $size");
header("Content-type: $type");
header("Content-Disposition: attachment; filename=$name");
echo $CV;

}
mysql_close();

?>

Tata letak basis data:

CREATE TABLE IF NOT EXISTS `participants` (
  `id_participants` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(30) NOT NULL,
  `area_of_exp` varchar(30) NOT NULL,
  `cv` blob NOT NULL,
  `File_Size` varchar(30) NOT NULL,
  `File_Type` varchar(30) NOT NULL,
  `File_Name` varchar(30) NOT NULL,
  PRIMARY KEY (`id_participants`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;

http://www.sqlfiddle.com/#!2/91cb3/4

Saya menyarankan Anda untuk tidak menyimpan CV di database Anda.



  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. MySQL GROUP BY/ORDER BY masalah

  2. Mysql:Temukan entri dari hari tertentu menggunakan kolom stempel waktu

  3. Membuat skrip melalui ssh melalui dempul di windows

  4. Hasil loop berdasarkan kategori tetapi hanya menampilkan kategori sekali

  5. Panjang maksimum untuk teks tipe MySQL