As you can see below I am pulling information from a database and writting it to a text file in a structured format (Thanks to everyone who has helped me out).
// Query the database for data
$query = "SELECT cards.card_id,title,description,meta_description,seo_keywords,price FROM cards,card_cheapest WHERE cards.card_id = card_cheapest.card_id ORDER BY card_id";
$result = mysql_query($query);
$result = mysql_query($query);
// Open file for writing
$myFile = "googleproducts.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
// Loop through returned data and write (append) directly to file
fprintf($fh, "%-10s %-50s %-450s %-50s %-300s\n", "id", "label","description","price","seo keywords");
fprintf($fh, "\n");
while ($row = mysql_fetch_assoc($result)) {
fprintf($fh, "%-10s %-50s %-450s %-50s %-300s\n", $row['card_id'], $row['title'], $row['description'],$row['price'], $row['seo_keywords']);
}
// Close out the file
fclose($fh);
?>
There are a few things that need to be added in it’s separate column that is not in the database. Mainly just bit of text like “bob’s cards” . I was wondering if it is possible to add that into the mix and have it loop through. Or is it just simpler to add it to the database?
You can have MySQL write the data directly to a file using
!! Note the use of forward slashes even on Windows !!
And please replace that ugly implicit
WHEREjoin by an explicitINNER JOIN; it will make future alterations to that query much easier to debug.Links: http://dev.mysql.com/doc/refman/5.1/en/select.html
And for the options to
SELECT .. INTO OUTFILEsee: http://dev.mysql.com/doc/refman/5.1/en/load-data.html