<html>
    <head>
    <title>Nutrient</title>
    </head>
    <body>
    <?
    $filName = "resource.txt";
    $objWrite = fopen($filName, "w");
    $objConnect = mysql_connect("localhost","u52030177","guillaume") or die("Error Connect to Database");
    $objDB = mysql_select_db("u52030177");
    $strSQL = "SELECT * FROM Nutrient";
    $objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");
    //$link = mysql_connect("localhost","u52030623","u52030623");
    //mysql_select_db("u52030623");
    mysql_query("SET NAMES UTF8");
    //$sql = mysql_query("select * from Nutrient;");
    //if(!$link) die('ERROR: '.mysql_error());
    ?>
    <h1 style="color:black;">Nutrient</h1>
    <table align=left width=100%>
    <?
    while($row = mysql_fetch_array($objQuery))
    {
    ?>
    <tr>
    <td>
    <div style="background-color:white; padding:5px; margin-bottom:5px;">
    <table>
    <tr>
    <td valign=top>
    <font color=blue><b><?=$row['FoodID']?></b></font><br>
    <font color=blue><b><?=$row['Protein']?></b></font><br>
    <font color=blue><b><?=$row['Fat']?></b></font><br>
    <font color=blue><b><?=$row['Carbohydrate']?></b></font><br>
    <font color=blue><b><?=$row['Sodium']?></b></font><br>
    <font color=blue><b><?=$row['Energy_Person']?></b></font><br>
    </td>
    </tr>
    </table>
    </div>
    </td>
    </tr>
    <?
    }
    ?>
    </table>
    <?
    while($objResult = mysql_fetch_array($objQuery))
    {
    fwrite($objWrite, "$objResult[Protein],$objResult[Fat],");
    fwrite($objWrite, "$objResult[Carbohydrate],$objResult[Sodium] \r\n");
    }
    echo "<br>Generate Text File Done.<br><a href=$filName>Download</a>";
    fclose($objWrite)
    ?>
    </body>
    </html>

i'm trying to download a file name "resource.txt" to my pc but when i click download nothing happens (atleast it should show the data i want in new page)

this is link : http://wangmuk.informatics.buu.ac.th/~52030177/index1.php

Recommended Answers

All 8 Replies

The file contents is not generated, because you try to loop the result set a second time, without resetting the internal file pointer. Uses mysql_data_seek before the second loop.

i added this but still nothing happens

<?
mysql_data_seek($objQuery);
while($objResult = mysql_fetch_array($objQuery))
{
    fwrite($objWrite, "$objResult[Protein],$objResult[Fat],");
    fwrite($objWrite, "$objResult[Carbohydrate],$objResult[Sodium] \r\n");
}
echo "<br>Generate Text File Done.<br><a href=$filName>Download</a>";
fclose($objWrite)
?>

Have you tried:

mysql_data_seek($objQuery, 0);

If that fails, just write the file inside the first loop.

<html>
<head>
<title>Nutrient</title>
</head>
<body>
<?$filName = "Nutrient.txt";
$objWrite = fopen("Nutrient.txt", "w");

$objConnect = mysql_connect("localhost","u52030177","guillaume") or die("Error Connect to Database");
$objDB = mysql_select_db("u52030177");
$strSQL = "SELECT * FROM Nutrient";
$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");

while($objResult = mysql_fetch_array($objQuery))
{
fwrite($objWrite, "$objResult[Protein],$objResult[Fat],");
fwrite($objWrite, "$objResult[Carbohydrate],$objResult[Sodium] \r\n");
}
fclose($objWrite);
echo "<br>Generate Text File Done.<br><a href=$filName>Download</a>";
?>
</table>
</body>
</html>

even this one doesnt work :(

Check your error log. If fopen fails it generates a warning.

how can i check error log? i run on ssh.

hey, i was trying to run my php on ssh and it works and a file is written. just when i try to download from browser it doesnt work.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.