Jump to content

Recommended Posts

Hello,

I am trying to write data to a txt file after reading from 2 database tables. I keep getting only 1 entry written to the g_file.txt. What am I missing here  ??? Still new to this, not sure if I am writing the code efficiently either  :) Thanks for any help!

$sql = "SELECT * FROM products_description";                                          
$result = mysql_query($sql, $connection) or trigger_error("SQL", E_USER_ERROR);
$num_rows = mysql_num_rows($result);
$sql2 = "SELECT * FROM products";                                                     
$result2 = mysql_query($sql2, $connection) or trigger_error("SQL", E_USER_ERROR);
$num_rows2 = mysql_num_rows($result2);

//heading to be written once to file
$f1 = fopen("g_file.txt", "w+");
fwrite($f1, "id\ttitle\tdescription\tprice\tlink\timage_link\n");

//loop to get data
while ($r2 = mysql_fetch_array($result2))           
{
$y4 = $r2["products_id"];
$y1 = $r2["products_model"];
$y2 = $r2["products_price"];
$y3 = $r2["products_image"];

	while ($r = mysql_fetch_array($result))            
			{
			$x1 = $r["products_name"];
			$x2 = $r["products_description"];
	 }
 }
$f2 = fopen("g_file.txt", "a+");
fwrite($f2, "$y4\t$x1\t$x2\t$y2\thttp://x.com/index.php?main_page=product_info&products_id=$y4\thttp://x.com/images/$y3\n");

fclose($f2); ///close the file
fclose($f1); ///close the file

Link to comment
https://forums.phpfreaks.com/topic/145957-fwrite-problem/
Share on other sites

You will only get the final result written.

 

You should open the file once only. Write your header row.

Then write to the file within the loop for your database records. Then close the file after the loop.

 

You are also using 2 queries and a nested loop, bad idea. You should write 1 query and join the 2 tables. I am guessing they join via products_id

 

SELECT p.*,pd.products_name,pd.products_description FROM products p, products_description pd WHERE p.products_id=pd.products_id

 

Now your loop

//heading to be written once to file
$f1 = fopen("g_file.txt", "w+");
fwrite($f1, "id\ttitle\tdescription\tprice\tlink\timage_link\n");

//loop to get data
while ($r2 = mysql_fetch_array($result2)) {
$y4 = $r2["products_id"];
$y1 = $r2["products_model"];
$y2 = $r2["products_price"];
$y3 = $r2["products_image"];

fwrite($f1, $y4."\t".$y1."\t".$y2."\t".$y3."\n");
}
fclose($f1);

Link to comment
https://forums.phpfreaks.com/topic/145957-fwrite-problem/#findComment-766277
Share on other sites

Because you should join tables on foreign keys. If for some reason there were 150 records in the products table and 151 in the description then you arent going to get the correct results. Also if you ordered one table in adifferent way to the other then the products wont match the descriptions.

Link to comment
https://forums.phpfreaks.com/topic/145957-fwrite-problem/#findComment-766912
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.