0

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

1 回答 1

1

我想我修好了,首先删除了文件下载部分,然后在屏幕上打印了查询。我看到变量从“锌指蛋白”截断为“锌”。我猜是post方法中的一些东西。然后我在 $sterm 上使用了单引号,同时在 post 方法中传递它,比如value= '$sterm'并且它起作用了。这是我用来传递变量的脚本:

echo "<table width='100%'><tr><td><strong>Click on column headers to sort; * dbTF = DNA binding Transcription Factor</strong></td> <td> 
<form action='search/download-search' method='post' >
<input type='hidden' name='sterm' value= '$sterm' >
<input type='submit' value='Download now'>
</form>
</td> </tr>
</table>";

我的想法多么愚蠢-一个查询给出了不同的结果:(感谢您的建议!

于 2013-02-13T10:58:31.893 回答