Eljay Posted February 19, 2009 Share Posted February 19, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/145957-fwrite-problem/ Share on other sites More sharing options...
JonnoTheDev Posted February 19, 2009 Share Posted February 19, 2009 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); Quote Link to comment https://forums.phpfreaks.com/topic/145957-fwrite-problem/#findComment-766277 Share on other sites More sharing options...
angelcool Posted February 19, 2009 Share Posted February 19, 2009 Check this out: http://www.phpfreaks.com/forums/index.php/topic,238878.msg1113369.html#msg1113369 NaCo Quote Link to comment https://forums.phpfreaks.com/topic/145957-fwrite-problem/#findComment-766332 Share on other sites More sharing options...
Eljay Posted February 19, 2009 Author Share Posted February 19, 2009 Thanks Neil for your response. The solution worked great! Why was it a bad idea to use 2 queries and a nested loop? Quote Link to comment https://forums.phpfreaks.com/topic/145957-fwrite-problem/#findComment-766360 Share on other sites More sharing options...
JonnoTheDev Posted February 20, 2009 Share Posted February 20, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/145957-fwrite-problem/#findComment-766912 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.