Blog IT : smartphone and computer technology

www.domainesia.com

Senin, 28 Desember 2020

Export data MySQL multiple table ke Excel dengan PHP

 <?php

mysql_connect("localhost","root","");
mysql_select_db("pmb_eksport");

$select = "SELECT pmb.kodereg, password, npassword, pmb.tahun165, pmb.nama, kelamin, tempatlahir, tanggallahir, 
				  agama, alamat, kota, rt, rw, kodepos, propinsi, email, foto 
					FROM pmb
						left join pmbform_jurusan on pmb.kodereg=pmbform_jurusan.kodereg 
							left join pmbreg on pmb.kodereg=pmbreg.kodereg where pmbform_jurusan.status='1'";

$export = mysql_query ( $select ) 
       or die ( "Sql error : " . mysql_error( ) );
$fields = mysql_num_fields ( $export );

for ( $i = 0; $i < $fields; $i++ ) {
	$header .= mysql_field_name( $export , $i ) . ",";
}

while( $row = mysql_fetch_row( $export ) ) {
	$line = '';
	foreach( $row as $value ) {
		if ( ( !isset( $value ) ) || ( $value == "" ) ){
			$value = ",";
		}
		else {
			$value = str_replace( '"' , '""' , $value );
			$value = '"' . $value . '"' . ",";
		}
		$line .= $value;
	}
	$data .= trim( $line ) . "\n";
}
$data = str_replace( "\r" , "" , $data );

header("Content-type: application/vnd.ms-excel");
header("Content-disposition: csv" . date("Y-m-d") . ".csv");
header( "Content-disposition: filename=report.csv");
print "$header\n$data";
exit;


EmoticonEmoticon