zeeshan_haider000 Posted December 27, 2010 Share Posted December 27, 2010 Hi there, I am trying to create a file using fwrite function, but for something reason all I get is a blank file with no content inside. Here is the code, go to the bottom where the fwrite function is called: <?php //Get Nohay Albums function function GetAlbums($cattype){ if ((isset($_GET['year']))||(isset($_GET['reciter']))){ //nothing displayed } else{ $query = "SELECT COUNT(Topic), SUM(Hits) AS totalhits, FileFormat, Speaker, Language, Year, FileType, Cat FROM nauhey WHERE Cat='".addslashes($cattype)."' AND Year='".date('Y')."' GROUP By Speaker, Year"; $result = mysql_query($query) or die (mysql_error()); //<!------------ Create Cache -------------!> //folder where cache is saved $path = "cache/"; //Page name format: FunctionName_Category_Year.html $pagename = $path."getalbums_".$cattype.".html"; $cacheFile = fopen($pagename, "w"); if ($cacheFile == FALSE){ die ("Unable to create cache file"); } //Get HTML in a variable $obstart = ob_start(); if ($obstart == FALSE){ die ("Unable to buffer cache content!"); } //<!------------ Cache closed -------------!> if ($cattype = strtolower('nauhey')){ while ($row = mysql_fetch_array($result)){ echo ("<div class='container'>"); echo ("<img src=\"http://localhost/HAQMOLAAALI/backed up/haqmolaali/images/".$row['FileFormat'].".gif\">"); echo ("<b style=\"font-size:10px; color:#000066; position:absolute; margin-left: 500px;\">DOWNLOADS: ".$row['totalhits']."</b>"); echo (" <a href=\"nohay.php?reciter=".urlencode($row['Speaker'])."&year=".urlencode($row['Year'])."\">".$row['Speaker']." Vol ".$row['Year']."</a> <b style=\"font-size:10px; color:#000000;\">(".strtoupper($row['Language']).")</b>"."\n"); echo ("<div class='small'><b>Year: ".$row['Year']." | Language: ".ucfirst($row['Language'])." | Tracks: ".$row['COUNT(Topic)']."</b></div>"); echo ("</div>"); }//while loop closed } else { while ($row = mysql_fetch_array($result)){ echo ("<div class='container'>"); echo ("<img src=\"http://localhost/HAQMOLAAALI/backed up/haqmolaali/images/".$row['FileFormat'].".gif\">"); echo ("<b style=\"font-size:10px; color:#000066; position:absolute; margin-left: 500px;\">DOWNLOADS: ".$row['totalhits']."</b>"); echo (" <a href=\"majlis.php?reciter=".urlencode($row['Speaker'])."&year=".urlencode($row['Year'])."\">".$row['Speaker']." Vol ".$row['Year']."</a> <b style=\"font-size:10px; color:#000000;\">(".strtoupper($row['Language']).")</b>"); echo ("<div class='small'><b>Year: ".$row['Year']." | Language: ".ucfirst($row['Language'])." | Lectures in series: ".$row['COUNT(Topic)']."</b></div>"); echo ("</div>"); } } //<!------------ Cache Code -------------!> $cacheContent = ob_get_contents(); //Write to cache file fwrite ($cacheFile, $cacheContent) or die ("Cannot write cache content to file!"); fclose ($cacheFile); ob_flush(); ob_end_clean(); //<!------------ Cache Closed-------------!> } } GetAlbums("nauhey"); ?> When I execute the function, I get my self-made error "Cannot write cache content to file!". Help would be much appreciated. Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/222713-ob_start-and-fwrite-problem-not-writing-to-file/ Share on other sites More sharing options...
zeeshan_haider000 Posted December 27, 2010 Author Share Posted December 27, 2010 When I remove the "while" loops from the script, the fwrite function works correctly and writes data to file. But I need the while loop to get data from database. Anyone know what the problem could be? Quote Link to comment https://forums.phpfreaks.com/topic/222713-ob_start-and-fwrite-problem-not-writing-to-file/#findComment-1151730 Share on other sites More sharing options...
PFMaBiSmAd Posted December 27, 2010 Share Posted December 27, 2010 You need to use mysql_num_rows() to find out if your query matched any rows. You should only process the results of the query if there are any rows and if you are getting zero rows, but you think your query should return something, you would need to troubleshoot why your query is not matching any rows. Quote Link to comment https://forums.phpfreaks.com/topic/222713-ob_start-and-fwrite-problem-not-writing-to-file/#findComment-1151771 Share on other sites More sharing options...
zeeshan_haider000 Posted December 27, 2010 Author Share Posted December 27, 2010 Hi, thanks for the reply @PFMaBiSmAd. You are right, I just checked and the query is returning zero rows. Let me check what's wrong with the query and see if i am able to solve the problem by myself. Quote Link to comment https://forums.phpfreaks.com/topic/222713-ob_start-and-fwrite-problem-not-writing-to-file/#findComment-1151930 Share on other sites More sharing options...
zeeshan_haider000 Posted December 27, 2010 Author Share Posted December 27, 2010 Okay, I finally found what the problem was, it was a stupid spelling mistake that I made when calling the function. GetAlbums("nauhey") should have been GetAlbums("nohay"). Now it's working properly. Spent so much time on this small silly mistake. Thanks for replying @ PFMaBiSmAd. Quote Link to comment https://forums.phpfreaks.com/topic/222713-ob_start-and-fwrite-problem-not-writing-to-file/#findComment-1151937 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.