I am using PHP and MySQL to run a query, I have written two scripts having same query, but one for displaying the query result on browser and other for downloading the query result as a file. Even though its the same query, its giving different result while downloading, which is incorrect. My query for instance is
$query="select * from tf where gene_symbol like '%$sterm%' || gene_name like '%$sterm%' || synonym like '%$sterm%'";
If $sterm
is ZINC FINGER PROTEIN, it give 685 records both on mysql command line and display. But if I download the file it give around 800 records by adding even the records where gene_name like '%ZINC FINGER%'
Does anyone have any idea, what could be causing it. Thanks in advance. Konika
Here is the code for downloading the file
<?php
$sterm = $_POST["sterm"];
$sterm = strtoupper($sterm);
$query = "select * from tf where gene_symbol like '%$sterm%' || gene_name like '%$sterm%' || synonym like '%$sterm%'";
$result = mysql_query($query) or die(mysql_error());
if (mysql_num_rows($result) > 0) {
result_disp($result);
}
function result_disp($results)
{
$num_fields = mysql_num_fields($results);
$headers = array();
for ($i = 0; $i < $num_fields; $i++) {
$headers[] = mysql_field_name($results, $i);
}
$fp = fopen('php://output', 'w');
if ($fp && $results) {
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="export.txt"');
header('Pragma: no-cache');
header('Expires: 0');
fputcsv($fp, $headers, chr(9));
while ($rows = mysql_fetch_row($results)) {
fputcsv($fp, array_values($rows), chr(9));
}
die;
}
}
fclose($fp);
?>