How to check if table exists in MySql db?

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>";

Was this solution helpful to you?

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

Just Added Q & A:

Find solution

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.