PHP coding for download database to CSV format

This is download database  in  CSV format.why we are  use this ? if  database can  be corrupted . you can use the  download and  import easy to database.

Coding for  view Database Value

<!DOCTYPE html>
<html lang="en">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
.nav{width:80%;margin:0 auto;font-family:Arial;}
.table{width:40%;margin:0 auto;}
<div class="nav">
<h2><a href="">Cracker World</a></h2>
<div class="table">
<p align="right"><a href="download.php">Download CSV</a></p>
<table border="1" cellspacing="0" cellpadding="0" width="100%">
<tr bgcolor="#306161">
<th width="12%" scope="col">S.No</th>
<th width="26%" scope="col">Reg NO</th>
<th width="41%" scope="col">Name</th>
<th width="21%" scope="col">Dept</th>
$sql = mysql_query("select * from employee");
while ($row = mysql_fetch_array($sql)) {
<tr align="center">
<td><?php echo $i++?></td>
<td><?php echo $reg_no;?></td>
<td><?php echo $name;?></td>
<td><?php echo $dept;?></td>
<?php } ?>

Coding for download CSV

$select_db = mysql_query("select * from employee");
$columns_total = mysql_num_fields($select_db);
while ($rows = mysql_fetch_array($select_db)) {
for ($i = 0; $i < $columns_total; $i++) {
$output .='"'.$rows["$i"].'",';
$output .="\n";
$save_filename = "employee.csv";
header('Content-type: application/csv');
header('Content-Disposition: attachment; filename='.$save_filename);
echo $output;