我为下载excel文件编写了一个PHP脚本,其中包含一些MySQL查询提取的MySQL数据。数据正确。这是样本图片。 Excel Sheet 现在问题是我得到两列'category_cod'和'material_cod'的代码(id),而不是我想从类别和材料表中获取category_name和material_name。如果有人知道如何实现这一点。这是我的代码:
<?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;
?>
答案 0 :(得分:0)
表bp bp_id material_cod category_cod
表材料 material_id material_name表类别 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