3

This method for exporting data on csv has worked previously on other projects, but I can not make this work on here, and I am not sure about how to enable erros for this case.

This PHP file creates a comma-separated file containing an initial row with a single tab ("ID"), and then it should be creating a row for each match on the SELECT query from DB

<?php
session_start();
ob_start();

include('conexionbbdd.php');

    $file='informes/expositores_'.time().'.xls';

    header("Content-Type: application/xls");    
    header("Content-Disposition: attachment; filename=$file");  
    header("Pragma: no-cache"); 
    header("Expires: 0");        

    $output = fopen($file, 'w');

    fwrite($output, chr(239) . chr(187) . chr(191)); 

    fputcsv($output, array('ID'), "\t");    

    // fetch the data
    $rows1 = mysqli_query($con, "SELECT ex_id FROM expositores WHERE ex_id = '26'");

    // loop over the rows, outputting them
    while ($row1 = mysqli_fetch_assoc($rows1)) {     
        fputcsv($output, $row1, "\t");        
    }

    fclose($output);
    echo $file;


ob_end_flush();   
?>

In this particular case I've simplified this to maximu so, apart from the initial row, a unique row containg the "26" should be created (I've tested that the query works with PhpMyAdmin, there's an ID 26). But it does not.

It only creates correctly first row from this first fputcsv method:

fputcsv($output, array('ID'), "\t");  

No other row seems to be fetched or placed on the CSV.

As the entire PHP file's aim is to create the CSV file, no error is shown because it does not open on a new window.

Output:

enter image description here

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Biomehanika
  • 1,530
  • 1
  • 17
  • 45
  • 2
    What is the question? Have you checked your error logs? Add error reporting to the top of your file(s) right after your opening ` – Jay Blanchard Jan 14 '16 at 13:18
  • 2
    I think instead of insert array you have to insert there values as `fputcsv($output, $row1['ex_id'], "\t"); ` – Saty Jan 14 '16 at 13:20
  • 1
    if `ex_id` is an int, then remove the quotes `WHERE ex_id = 26");` – Funk Forty Niner Jan 14 '16 at 13:22
  • 1
    @JayBlanchard that made me solve the issue. I was using myslqi on a mysql project. I did not know how to view errors on this case. Please type that as an answer to tick and explain for other users. – Biomehanika Jan 14 '16 at 13:29
  • @Saty, the file creates correctly the CSV as seen on the question. The mistake was on using mysqli and not knowing how to check errors on these cases. – Biomehanika Jan 14 '16 at 13:32
  • 1
    actually this question should be closed with http://stackoverflow.com/questions/17498216/can-i-mix-mysql-apis-in-php – Funk Forty Niner Jan 14 '16 at 13:33
  • @Fred-ii- what about changing the title to focus it on PHP error viewing? That would make this question useful for the community. – Biomehanika Jan 14 '16 at 13:34
  • well, *technically speaking*, the question would be a duplicate of "Mixing MySQL APIs". Since the connection details are/were unknown, then I think we should just leave well enough alone. You can change the title if you wish, however mixing MySQL APIs is the real issue here. – Funk Forty Niner Jan 14 '16 at 13:39
  • Yes, this question would be related to that one, but sincerelly my problem was about nort knowing the issue reason (solved after error enabling) rather than ignoring possibilities of mixing MySQL APIs. If this must be deleted/marked as duplicate please proceed. Thank you. – Biomehanika Jan 14 '16 at 13:43
  • 1
    No, like I said; let's leave it the way it is. This one's a bit tricky but I like the question. Your code could be useful for others. @Biomehanika – Funk Forty Niner Jan 14 '16 at 13:45
  • Note that you're also writing a CSV file and pretending it's XLS. This *will* bite you, sooner or later. – Piskvor left the building Jan 14 '16 at 14:06
  • Uhm... I've used this system for a year without issues, the only thing is that you have to open thje file trough the OPEN menu on Excel, in order to tabulate each coma-separated value on each tab. Have you got any idea of which kind of problems could this cause? – Biomehanika Jan 14 '16 at 14:11

1 Answers1

2

In order to solve this you will need to be able to view the errors. You can have a look in your error logs or add error reporting to the top of your file(s) right after your opening <?php tag error_reporting(E_ALL); ini_set('display_errors', 1);

Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119