<?

function mysqlGetData($sql){

    
ob_start();
    
    if((
$result=mysql_query($sql))){ 
     
        while((
$rs=mysql_fetch_array($result))){
            echo 
$rs[0];
            
//your results printed
    
        

    }
    
    
$output ob_get_contents(); 
    
ob_end_flush();
    
//all "echo" and "print" are beeing send to var "$output"
    // next time we have a file ready instead of having to query mysql
        
    
$file = @fopen("cache/".$sql.".txt""w+"); 
    
//we write it to a new file named after the $sql query
    
@fputs($file$output); 


}

//we start here
$sql "select * from database order by id desc  LIMIT 0, 50";

//we try to open a file named after the sql query
$fp = @fopen("cache/".$sql.".txt","r");

if (
$fp)
    {
    
    
$filemod filemtime("cache/".$sql.".txt");
    
//date of the file
    
$filemodtime date("Ymd"$filemod);
    
//what date is it now
    
$now time();
    
$now strtotime($time);
    
$nowtime date("Ymd"$now);
    
$difference $nowtime $filemodtime;
    
    if(
$difference >= 1){
        
//older then 1 day? get new
        
print "<!-- non cached  $filemodtime - $nowtime = $diverence -->";
        
mysqlGetData($sql);
    }
    else
    {
        
//get it from cache!
        
print "<!-- cached  $filemodtime - $nowtime = $diverence -->";
        
$buff fread($fp,1000000);
        print 
$buff;
    }
}
else

    
//file does not exist. so we try to create the file by starting the function
    
print "<!-- error non cached  $filemodtime - $nowtime = $diverence -->";
    
mysqlGetData($sql);
}

?>