I have created sql view. I want to export the view contents to excel sheet upon clicking the button say "Export in Excel". How do I do that?. Looking forward to your help.
<?php
$db_host = '';
$db_user = '';
$db_pwd = '';
$database = '';
$table = '';
if (!mysql_connect($db_host, $db_user, $db_pwd))
die("Can't connect to database");
if (!mysql_select_db($database))
die("Can't select database");
// sending query
$result = mysql_query("SELECT * FROM view_abc");
if (!$result) {
die("Query to show fields from table failed");
}
$fields_num = mysql_num_fields($result);
echo "<div class='wrapper'>
<font size=3 color=blue><b>Entry";
echo "</font></b><table class='table'><tr>";
// printing table headers
for($i=0; $i<$fields_num; $i++)
{
$field = mysql_fetch_field($result);
echo "<td><b>{$field->name}</b></td>";
}
echo "</tr>\n";
// printing table rows
while($row = mysql_fetch_row($result))
{
echo "<tr>";
// $row is array... foreach( .. ) puts every element
// of $row to $cell variable
/* foreach($row as $cell)
echo "<td>$cell</td>";
*/
echo "<td bgcolor='#d5eaf0'>{$row[0]}</td>";
echo "<td bgcolor='#d5eaf0'>{$row[1]}</td>";
if ($row[2]==0)
echo "<td bgcolor='green'>{$row[2]}</td>";
else if ($row[2]>0)
echo "<td bgcolor='red'>{$row[2]}</td>";
if ($row[3]==0)
echo "<td bgcolor='green'>{$row[3]}</td>";
else if ($row[3]>0)
echo "<td bgcolor='red'>{$row[3]}</td>";
if ($row[4]>90)
echo "<td bgcolor='green'>{$row[4]}</td>";
else if ($row[4]<90)
echo "<td bgcolor='red'>{$row[4]}</td>";
if ($row[5]>90)
echo "<td bgcolor='green'>{$row[5]}</td>";
else if ($row[5]<90)
echo "<td bgcolor='red'>{$row[5]}</td>";
if ($row[6]>90)
echo "<td bgcolor='green'>{$row[6]}</td>";
else if ($row[6]<90)
echo "<td bgcolor='red'>{$row[6]}</td>";
if ($row[7]>90)
echo "<td bgcolor='green'>{$row[7]}</td>";
else if ($row[7]<90)
echo "<td bgcolor='red'>{$row[7]}</td>";
if ($row[8]>90)
echo "<td bgcolor='green'>{$row[8]}</td>";
else if ($row[8]<90)
echo "<td bgcolor='red'>{$row[8]}</td>";
if ($row[9]>90)
echo "<td bgcolor='green'>{$row[9]}</td>";
else if ($row[9]<90)
echo "<td bgcolor='red'>{$row[9]}</td>";
if ($row[10]=='YES')
echo "<td bgcolor='green'>{$row[10]}</td>";
else if ($row[10]=='NO')
echo "<td bgcolor='red'>{$row[10]}</td>";
if ($row[11]==0)
echo "<td bgcolor='green'>{$row[11]}</td>";
else if ($row[11]>0)
echo "<td bgcolor='red'>{$row[11]}</td>";
if ($row[12]==0)
echo "<td bgcolor='green'>{$row[12]}</td>";
else if ($row[12]>0)
echo "<td bgcolor='red'>{$row[12]}</td>";
if ($row[13]<28)
echo "<td bgcolor='green'>{$row[13]}</td>";
else if ($row[13]>28)
echo "<td bgcolor='red'>{$row[13]}</td>";
echo "<td bgcolor='#d5eaf0'>{$row[14]}</td>";
echo "</tr></div>\n";
}
mysql_free_result($result);
?>
use the below query
$res = mysql_query("SELECT * INTO OUTFILE '$name.csv' FROM users ");
here $name is the name of the file you want to create.
otherwise you can use the present date by using date function instead of a static name. The file will be stored in xampp/mysql/data/your database name ( assuming you are using xampp).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.