Steven Matthews
Steven Matthews

Reputation: 11355

Echo is not giving me output

Here's my code:

$values = mysql_query("SELECT cult_desc FROM culture");

while ($rowr = mysql_fetch_row($values)) {
    for ($j=0;$j<$i;$j++) {
        $csv_output .= $rowr[$j]."; ";
    }
    $csv_output .= "\n";
}

$filename = $file."_".date("d-m-Y_H-i",time());
#echo $filename;
echo $csv_output;

As far as I can tell, it should go through each piece of data, echo it with a ";" and then a newline. Instead, it gives me no output.

Upvotes: 0

Views: 151

Answers (3)

Shiplu Mokaddim
Shiplu Mokaddim

Reputation: 57690

From the variable name ($csv_output) it seems you need CSV formatted output.

If you have FILE privilege why not invoke,

$values = mysql_query("SELECT cult_desc INTO OUTFILE '/tmp/result.txt'
  FIELDS TERMINATED BY ';'
  LINES TERMINATED BY '\n'
  FROM culture");
readfile("/tmp/result.txt");

Otherwise following code will do it.

$values = mysql_query("SELECT cult_desc FROM culture");
$csv_output = "";
while ($rowr = mysql_fetch_row($values)) {
    $csv_output .= implode(";", $rowr). "\n";
}
$filename = $file."_".date("d-m-Y_H-i",time());
#echo $filename;
echo $csv_output;

Upvotes: 1

prdatur
prdatur

Reputation: 1007

First of all mysql_fetch_row has in your case just one key its 0 (there the cult_desc field is stored).

You can use your code like this:

$csv_output = "";
$values = mysql_query("SELECT cult_desc FROM culture");
while ($rowr = mysql_fetch_assoc($values)) {
    $csv_output .= $rowr['cult_desc']."; \n";
}

$filename = $file."_".date("d-m-Y_H-i",time());
#echo $filename;
echo $csv_output;

i replaced mysql_fetch_row with mysql_fetch_assoc so you get the field name as the key not a number. With that you can direct access with cult_desc

Upvotes: 0

Cyclone
Cyclone

Reputation: 18305

Try initializing $csv_output with an empty string so it has something to concatenate onto. Otherwise you may get a notice, and your code won't work. Additionally, check to make sure there are actually values in your results.

$csv_output = '' ought to do the trick, call that before your loop unless you initialize it elsewhere.

You also need to replace $i with $j unless you have $i declared elsewhere?

Upvotes: 0

Related Questions