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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.