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

12
  • 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 <?php tag error_reporting(E_ALL); ini_set('display_errors', 1); Commented Jan 14, 2016 at 13:18
  • 2
    I think instead of insert array you have to insert there values as fputcsv($output, $row1['ex_id'], "\t"); Commented Jan 14, 2016 at 13:20
  • 1
    if ex_id is an int, then remove the quotes WHERE ex_id = 26"); Commented Jan 14, 2016 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. Commented Jan 14, 2016 at 13:29
  • 1
    actually this question should be closed with stackoverflow.com/questions/17498216/… Commented Jan 14, 2016 at 13:33

1 Answer 1

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);

Sign up to request clarification or add additional context in comments.

1 Comment

After enabling errors, it was clear that I was using mysqli on a mysql project.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.