Problem when exporting MySQL Database to Excel with PHPExcel

0

I'm trying to export the table from my mysql database using the PHPExcel library, but it does not generate anything for me.

I have this link with which I make the call to the export file:

<a href="exportar.php">Descargar tabla</a>

and I have this file exportar.php:

<?php

error_reporting(E_ALL);
ini_set("display_errors", 1);

 $conexion = mysqli_connect ("localhost", "xxxx", "xxxx");
 mysqli_select_db ("xxxxx", $conexion);

 $sql = "SELECT * FROM solicitudes ORDER BY folio DESC";
 $resultado = mysqli_query ($sql, $conexion) or die (mysqli_error ());
 $registros = mysqli_num_rows ($resultado);

 if ($registros > 0) {
   require_once 'PHPExcel/Classes/PHPExcel.php';
   $objPHPExcel = new PHPExcel();

   //Informacion del excel
   $objPHPExcel->
    getProperties()
        ->setCreator("lahuerta")
        ->setLastModifiedBy("lahuerta")
        ->setTitle("Exportar Base de Datos")
        ->setSubject("Tabla")
        ->setDescription("Documento generado con PHPExcel")
        ->setKeywords("lahuerta  con  phpexcel")
        ->setCategory("solicitudes");    

   $i = 1;    
   while ($registro = mysqli_fetch_object ($resultado)) {

      $objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A'.$i, $registro->name);

      $i++;

   }
}
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="tabla.xlsx"');
header('Cache-Control: max-age=0');

$objWriter=PHPExcel_IOFactory::createWriter($objPHPExcel,'Excel2007');
$objWriter->save('php://output');
exit;
mysql_close ();
?>

At the time of execution, he shows me the following warnings:

Warning: mysqli_select_db() expects parameter 1 to be mysqli, string given in /home/exportar.php on line 7

Warning: mysqli_query() expects parameter 1 to be mysqli, string given in /home/exportar.php on line 10

Warning: mysqli_error() expects exactly 1 parameter, 0 given in /home/exportar.php on line 10
    
asked by Sharly Infinitywars 21.07.2017 в 20:08
source

1 answer

1

Errors arise because the order of the parameters passed to the functions mysqli_select_db , mysqli_query and mysqli_error is incorrect.

The solution is simply to pass $conexion as the first parameter.

Example:

mysqli_select_db ($conexion, "xxxxx");
$resultado = mysqli_query ($conexion, $sql) or die (mysqli_error ($conexion));
    
answered by 21.07.2017 / 20:11
source