-1

I am trying to run the following code

$query = sprintf('SELECT * FROM custfail');
$result = mysql_query($query, $conn) or die(mysql_error($conn));

/*
 * send response headers to the browser
 * following headers instruct the browser to treat the data as a csv file called export.csv
 */

header('Content-Type: text/csv');
header('Content-Disposition: attachment;filename=custfail.csv');

/*
 * output header row (if atleast one row exists)
 */

$row = mysql_fetch_assoc($result);
if ($row) {
    echocsv(array_keys($row));
}

/*
 * output data rows (if atleast one row exists)
 */

while ($row) {
    echocsv($row);
    $row = mysql_fetch_assoc($result);
}

/*
 * echo the input array as csv data maintaining consistency with most CSV implementations
 * - uses double-quotes as enclosure when necessary
 * - uses double double-quotes to escape double-quotes 
 * - uses CRLF as a line separator
 */

function echocsv($fields)
{
    $separator = '';
    foreach ($fields as $field) {
        if (preg_match('/\\r|\\n|,|"/', $field)) {
            $field = '"' . str_replace('"', '""', $field) . '"';
        }
        echo $separator . $field;
        $separator = ',';
    }
    echo "\r\n";

When I run the code i get a CSV file with the following error located in it

<b>Fatal error</b>:  Call to undefined function echocsv() in <b>*snip*</b> on line <b>31</b><br />

The line number is unrelated to the code posted as I have removed some previous code (a password check if statement(that this code is embedded in) and SQL connection details) but it is happening when calling this function,

$row = mysql_fetch_assoc($result);
if ($row) {
    echocsv(array_keys($row));
}

Is anyone able to point me in the right direction

18
  • try to move your function to top of the page
    – Ram Sharma
    Sep 17, 2014 at 9:52
  • 1
    Missing } after the end of the function is probably the issue.. although I would have expected PHP to throw a parse error if that was the actual code you used. Sep 17, 2014 at 9:53
  • @RamSharma no it isn't - there's closing tags for the if and foreach, not for the function itself. Sep 17, 2014 at 9:55
  • The function needs to be before the call, PHP executes it's script top to bottom.
    – Edward
    Sep 17, 2014 at 9:56
  • @ɴᴀᴛʜ I agree, if functions is not closed than error would be parse error. In this case he has to move his function top of his page
    – Ram Sharma
    Sep 17, 2014 at 9:57

2 Answers 2

0

Thanks to the help of @RamSharma in his comments on the question. the issue was that I had the function at the bottom of the page, after it was being called

The solution was to move the function to lines above the call

-1

If you are using a "raw" *.php script, you need to define the function before the call, base on the reading here: http://php.net/manual/en/functions.user-defined.php

"Functions need not be defined before they are referenced, except when a function is conditionally defined(...)"

2
  • 2
    Functions need not be defined before they are referenced.
    – Ja͢ck
    Sep 17, 2014 at 10:05
  • @Ja͢ck is right, my mistake, the issue had to do with the function being misplaced though anyway, since we're missing code in the question. Sep 17, 2014 at 10:47

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.