Jump to content

Problem writing MySQL table to text file


sinisnap

Recommended Posts

hi i'm using the code below to extract data from a mysql db and first show it in a table in the browser and also update/write to a text file on the server. the first part is working fine. the second part..although it doesn't give me an error, the orders.txt file remains completely blank. so i'm assuming there's some problem with the fopen/fwrite part of the script? can anyone help? thank you!!

 

<?php

 

require("../elements/global-vars.inc.php");

require("../elements/database-connection.inc.php");

 

/* Build your SQL statement however you need. */

$sql = "SELECT * FROM orders, users WHERE filled = 'false' AND orders.userID = users.userID ORDER BY date";

 

/* Execute the query. */

if (!$res = @mysql_query($sql)) {

die(mysql_error());

return false;

}

/* Create an array of arrays out of the recordset. */

while ($row = mysql_fetch_array($res, MYSQL_ASSOC)) {

$data[] = $row;

}

 

 

 

 

/* Now iterate through the recordset creating a simple table. */

echo "<style>table.dump { font-family:Arial; font-size:10pt; }</style>";

echo "<table class=\"dump\" border=\"1\" cellpadding=\"1\" cellspacing=\"0\">\n";

echo "<tr>";

echo "<th>#</th>";

foreach($data[0] as $key=>$val) {

echo "<th><b>";

echo $key;

echo "</b></th>";

}

echo "</tr>\n";

$row_cnt = 0;

foreach($data as $row) {

$row_cnt++;

echo "<tr align='center'>";

echo "<td>".$row_cnt."</td>";

foreach($row as $val) {

echo "<td>";

echo $val;

echo "</td>";

}

echo"</tr>\n";

}

echo "</table>\n";

 

 

 

 

/*Next, you need to know how to save the recordset back into a TAB delimited file. This example uses the $data value from the code example above. */

 

 

$fp = fopen('orders/orders.txt', 'w') or die("ERROR: can't open file for writing");;

 

foreach($data as $row) {

 

$line = "";

 

foreach($row as $val) {

$line .= "\t\"".$val."\"";

}

 

/* Strip off the first TAB and add a carriage return. */

$line = substr($line, 1)."\n";

 

$fwrite($fp, $line);

}

 

fclose($fp);

 

?>

 

Link to comment
https://forums.phpfreaks.com/topic/81499-problem-writing-mysql-table-to-text-file/
Share on other sites

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.