Jump to content

[SOLVED] final question : how to save a querys result into a file


jber

Recommended Posts

 

Well , a final question. I´ve implemented a server in which people can log and fill surveys.

As an external admin i want to save the result of the query (the query consists in viewing some fields of the table that people fills) into a file so i can send it via mail to the person i want.

 

I´ve got the query results in a php page, and presented via table (using html) so i´d like to save this into an html page

 

echo "<table bgcolor=\"#DDDDDD\" align=center style=\"border:2px outset black\">";
for ($i = 0; $i < mysql_num_fields($result); $i++)
    { 
    print "<th>".mysql_field_name($result, $i)."</th>\n"; 
    } 
while ($registro = mysql_fetch_row($result))
    {
    echo "<tr>";
    foreach($registro  as $clave)
        {
        echo "<td bgcolor=\"#BBBBBB\"style=\"border:2px groove black\" align=\"center\">",$clave,"</td>";
        }
    }
echo "</tr></table>";

 

Can you help me? I´ve implemented the save as option in javascript, but i want my application to save the page automatically without asking anything but filename (and this is even optional for me)

 

Thanks in advance

 

Another option that works partially is

 


$query2 = " SELECT " .$select_cols . " FROM " .$_SESSION["nomtab"]. " INTO OUTFILE 'textfilename.txt' FIELDS TERMINATED BY ',' ENCLOSED BY '--' LINES TERMINATED BY '\n' ";

 

It does write the results in the file, but i want, to improve the application, to display row by row and if possible started by field´s name

 

I attach the code for this page so you can view it better . What is not in english are variable names

 

Thanks in advance

 

[attachment deleted by admin]

Try this:


$pageData = "<table bgcolor=\"#DDDDDD\" align=center style=\"border:2px outset black\">";

for ($i = 0; $i < mysql_num_fields($result); $i++)
    { 
    $pageData .= "<th>".mysql_field_name($result, $i)."</th>\n"; 
    } 
while ($registro = mysql_fetch_row($result))
    {
    $pageData .= "<tr>";
    foreach($registro  as $clave)
        {
        $pageData .= "<td bgcolor=\"#BBBBBB\"style=\"border:2px groove black\" align=\"center\">",$clave,"</td>";
        }
    }
$pageData .= "</tr></table>";


$fileName = "pagedata.txt";
$location = "output/path"; // relative path name
$fullPath = $location . "/" . $fileName;

// Check to see if the directory exist. If not, create it.
if(!is_dir($location)) {
   mkdir($location);
   // default permission "0777" (all permissions, all users, which is probably what you want)
}

// Open up the file. (If the file exists, this will append the data to the file)
// You can prevent this by adding a unique identifier to $fileName, such as the data and time, ie:
// $fileName = "Output" . date("Y_M_j_H_s"); 
if (!$handle = fopen( $fullPath, "a")) {

     echo "Could not create file";
     exit;

} else {
     // ***
     // This is the line that actually writes the file
     // *** 
     if(fwrite($handle,$pageData )===FALSE) {
          echo "Could not write to file";
          exit;
     }

     fclose($handle);
}


NOTE

This code is a modified version of the code posted by Elliott Brueggeman on php.net

(http://us.php.net/manual/en/function.fwrite.php)

 

i´ve tried your solution but it does not work, it outputs an error that i can´t solve

 

the error is : Parse error: syntax error, unexpected $end in C:\wamp\www\primeros_pasos\resultadofinal.php on line 103

 

I´ve searched if i have missed an ending of </html> or something like that, but i can´t find the error. Please, can you help me?

 

I attach the code

 

 

[attachment deleted by admin]

 

Now it partially works. It do writes the name of each field in a html page (because i save the query in a html file, including the headers like this :

 


$pageData = "<html><body><table bgcolor=\"#DDDDDD\" align=center style=\"border:2px outset black\">";


 

but it does not write any line of the results, like it does not enter in the while loop

Could it be the condition? Thanks in advance for your help

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.