Webocreation

Friday, March 25, 2011

CSV import /export code






$db_hostname = 'localhost';

$db_username = 'root';

$db_password = '';

$db_name = 'db_csvtest';
$db_connection = mysql_connect($db_hostname, $db_username, $db_password) or die("Error:=>Could not connect");

mysql_select_db($db_name);
?>






Untitled Document



<?php

  include('dbconnect.php');

  ?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

  <html xmlns="http://www.w3.org/1999/xhtml">

  <head>

  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

  <title>Export MYSQl Data to CSV/Excel file in PHP</title>

  <link rel="stylesheet" type="text/css" href="css/style.css" />

  </head>

<body>

  <div align="center" style="padding:20px;">

<table  width="100%"  cellpadding="0"  cellspacing="0" border="0">

  <tbody>

  <tr>

  <td>

  <strong>Exporting MySQL data  into CSV/Excel file in PHP</strong>

  </td>

  </tr>

  <tr>

  <td>

  <div  style="overflow:scroll;  height:400px; width:300px; margin-top:20px;">

  <?php

  $result = mysql_query("SELECT *    FROM tbl_country ORDER BY country_id ASC");

  $row_data = mysql_fetch_array($result);

  while($row_data = mysql_fetch_array($result)) {

  ?>

 

  <div style="padding:0px 0px 2px 0px; border: 1px medium #999999;"><?php echo $row_data['country_id']?>  -  <?php echo $row_data['country_name']?>  - <?php echo $row_data['country_code']?> </div>

 

  <?php } ?>

  </div>

 

 

  </td>

  </tr>

  <tr>

  <td style="padding-top:20px; font-weight:bold;">

  <a href="export_data.php?filename=country_list">Export Data to Excel File</a>

  </td>

  </tr>

  <tr>

  <td style="padding-top:20px; font-weight:bold;">

  <a href="csv.php?filename=country_list">Export Data to CSV File (with table fieldname as header)</a>

  </td>

  </tr>

  </tbody>

  </table>

</div>


<?php

  mysql_close($db_connection);

  ?>

  </body>

  </html>







No comments:

Post a Comment