I have written a PHP script for download excel file which contain some MySQL data fetching by a MySQL query. Data is coming correctly. Here is the sample picture. Excel Sheet Now the problem is i am getting codes(id) of two column 'category_cod' and 'material_cod' instead I want to fetch category_name and material_name from category and material tables. Please If anyone have any idea how to achieve that. Here is my code:
<?php
$header = '';
$data = '';
$select = "select * from pdbp inner join bp on(pdbp.bp_id=bp.id)";
//run mysql query and then count number of fields
$export = mysql_query ( $select )
or die ( "Sql error : " . mysql_error( ) );
$fields = mysql_num_fields ( $export );
//create csv header row, to contain table headers
//with database field names
for ( $i = 0; $i < $fields; $i++ ) {
$header .= mysql_field_name( $export , $i ) . ",";
}
//this is where most of the work is done.
//Loop through the query results, and create
//a row for each
while( $row = mysql_fetch_row( $export ) ) {
$line = '';
//for each field in the row
foreach( $row as $value ) {
//if null, create blank field
if ( ( !isset( $value ) ) || ( $value == "" ) ){
$value = "\t";
}
//else, assign field value to our data
else {
$value = str_replace( '"' , '""' , $value );
$value = '"' . $value . '"' . "\t";
}
//add this field value to our row
$line .= $value;
}
//trim whitespace from each row
$data .= trim( $line ) . "\n";
}
//remove all carriage returns from the data
$data = str_replace( "\r" , "" , $data );
$file_name = 'excel_data';
//create a file and send to browser for user to download
header("Content-type: application/vnd.ms-excel");
header( "Content-disposition: filename=".$file_name.".xls");
print "$header\n$data";
exit;
?>
Table bp bp_id material_cod category_cod
table materials material_id material_name
table categories category_id category_name
SELECT material_name, category_name FROM bp
LEFT JOIN materials ON materials.material_id = bp.material_cod
LEFT JOIN categories ON categories.categories_id = bp.category_cod
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.