How can I update a MySql table based on another using PHP?
-
Using the 'product_id' in Table A, I would like to check if a product exists in Table B. If the product does exist, I want to replace the 'product_price' in Table B with the one from Table A. I am currently using a 'while' loop in PHP, checking each product id and replacing the prices 1 by 1. However it is really slow for 28,000 products. Is there a faster and more efficient way? Current Code: echo "<b>Price Update Begin:</b><br/></br>"; $updates = mysql_query("SELECT list_price, product_code FROM new_xml"); while ($currentRow = mysql_fetch_assoc($updates)){ $oldPrice = mysql_num_rows("SELECT product_code FROM master_xml WHERE product_code = '".$currentRow['product_code']."'"); if($oldPrice == 1){ mysql_query("UPDATE master_xml SET list_price = '".$currentRow['list_price']."'"); echo "Changed Price of ".$currentRow['product_code']." to ".$currentRow['list_price']."<br/>"; } else{ echo "No Price Was Changed.<br/>"; } } echo "<br/><b>Price Update End!</b><br/></br>";
-
Answer:
For one I think your '$oldPrice = mysql_num_rows' line is going to cause you issues. You aren't counting rows from a query result, but from a text string that isn't really doing anything. -- Revised -- echo "<b>Price Update Begin:</b><br/></br>"; $updates = mysql_query("SELECT list_price, product_code FROM new_xml"); while ($currentRow = mysql_fetch_assoc($updates)){ $result = mysql_query("SELECT product_code FROM master_xml WHERE product_code = '$currentRow[product_code]'"); $oldPrice = mysql_num_rows($result); if($oldPrice == 1){ mysql_query("UPDATE master_xml SET list_price = '$currentRow[list_price]'"); echo "Changed Price of ".$currentRow['product_code']." to ".$currentRow['list_price']."<br/>"; } else { echo "No Price Was Changed.<br/>"; } } echo "<br/><b>Price Update End!</b><br/></br>";
[email protected] at Yahoo! Answers Visit the source
Other answers
For one I think your '$oldPrice = mysql_num_rows' line is going to cause you issues. You aren't counting rows from a query result, but from a text string that isn't really doing anything. -- Revised -- echo "<b>Price Update Begin:</b><br/></br>"; $updates = mysql_query("SELECT list_price, product_code FROM new_xml"); while ($currentRow = mysql_fetch_assoc($updates)){ $result = mysql_query("SELECT product_code FROM master_xml WHERE product_code = '$currentRow[product_code]'"); $oldPrice = mysql_num_rows($result); if($oldPrice == 1){ mysql_query("UPDATE master_xml SET list_price = '$currentRow[list_price]'"); echo "Changed Price of ".$currentRow['product_code']." to ".$currentRow['list_price']."<br/>"; } else { echo "No Price Was Changed.<br/>"; } } echo "<br/><b>Price Update End!</b><br/></br>";
Chris B
Related Q & A:
- How can I make a dynamic web page in PHP?Best solution by Yahoo! Answers
- In Visual Studio 2012 or 2013, how can I register a DLL file on Windows 7 64-bit computer using a Custom Action command?Best solution by Stack Overflow
- How can I parse a complex XML with PHP and CDATA?Best solution by Stack Overflow
- How can I email a photo on Yahoo email without using an attachment?Best solution by Yahoo! Answers
- How can I get a home based online job?Best solution by forbes.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.