<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
// Database configuration 
$dbHost     = "localhost"; 
$dbUsername = "root"; 
$dbPassword = '3$Par74N0;urp'; 
$dbName     = "urp_admision"; 
 
// Create database connection 
$db = new mysqli($dbHost, $dbUsername, $dbPassword, $dbName); 
 
// Check connection 
if ($db->connect_error) { 
    die("Connection failed: " . $db->connect_error); 
}
 
// Fetch records from database 
$query = $db->query(" 
SELECT 
	  camp0
	, cod_al
	, nombre
	, apelli1
	, telef1
	, telef2
	, email
	, edad
	, sexo
	, colegio
	, ciudad
	, direccion
	, avenida
	, distrito
	, apelli2
	, carrera1
	, carrera2
	, fecha 
FROM alumnos  
"); 
 
if($query->num_rows > 0){ 
    $delimiter = ";"; 
    $filename = "admisiones-data_" . date('Y-m-d') . ".csv"; 
     
    // Create a file pointer 
    $f = fopen('php://memory', 'w'); 
     
    // Set column headers 
    $fields = array(
	  'camp0'
	, 'cod_al'
	, 'nombre'
	, 'apelli1'
	, 'telef1'
	, 'telef2'
	, 'email'
	, 'edad'
	, 'sexo'
	, 'colegio'
	, 'ciudad'
	, 'direccion'
	, 'avenida'
	, 'distrito'
	, 'apelli2'
	, 'carrera1'
	, 'carrera2'
	, 'fecha'
	); 
    fputcsv($f, $fields, $delimiter); 
     
    // Output each row of the data, format line as csv and write to file pointer 
    while($row = $query->fetch_assoc()){ 
       
      $lineData = array(
	  $row['camp0']
	, $row['cod_al']
	, $row['nombre']
	, $row['apelli1']
	, $row['telef1']
	, $row['telef2']
	, $row['email']
	, $row['edad']
	, $row['sexo']
	, $row['colegio']
	, $row['ciudad']
	, $row['direccion']
	, $row['avenida']
	, $row['distrito']
	, $row['apelli2']
	, $row['carrera1']
	, $row['carrera2']
	, $row['fecha']
		); 
        fputcsv($f, $lineData, $delimiter); 
    } 
     
    // Move back to beginning of file 
    fseek($f, 0); 
     
    // Set headers to download file rather than displayed 
    header('Content-Type: text/csv'); 
    header('Content-Disposition: attachment; filename="' . $filename . '";'); 
     
    //output all remaining data on a file pointer 
    fpassthru($f); 
} 




 