[英]Nested SQL query in PHP while-loop causes Memory Leak - What are some other solutions?
Explanation: I'm attempting to transfer pricing on items from one table to another by comparing code to partnumber and updating the row. 说明:我正在尝试通过将代码与零件号进行比较并更新行来将商品的价格从一张表转移到另一张表。 The temporary table contains:
临时表包含:
code price
M1 1.23
M2 4.56
M3 7.89
And the second table contains: 第二张表包含:
partnumber price
M2 0.00
M3 0.00
M1 0.00
The issue is that I haphazardly nested a mysql_query inside of a while loop, so I was getting a memory leak. 问题是我在一个while循环内随意地嵌套了一个mysql_query,所以我遇到了内存泄漏。 I have around 70,000 rows of data and it bombed out at around row 160. T_T How should I go about bringing the data from the temporary table temp_yahooprices to combined_stock?
我有大约70,000行数据,它在第160行附近被炸毁。T_T我应该如何将数据从临时表temp_yahooprices移到Combined_stock? Keep in mind that the server only runs PHP4 so I cannot write prepared PDO statements.
请记住,该服务器仅运行PHP4,因此我无法编写准备好的PDO语句。 The pertinent code is pasted below (and the SQL query is commented out):
相关代码粘贴在下面(SQL查询已注释掉):
//Import Price Data
echo 'Attempting to write Yahoo pricing data...
<div style="margin:20px; border:1px solid #C0C0C0; overflow:scroll; height:200px; width:50%;">';
$query = "CREATE TABLE temp_yahooprices
AS (SELECT DISTINCT
Code AS code,
`Sale-price` AS price
FROM nteproject.ynmdataz
ORDER BY Code);";
$result = mysql_query($query) or die('ERROR: Could not create temporary Yahoo pricing table. ' . mysql_error() . '<br>');
$query = "SELECT * FROM temp_yahooprices";
$result = mysql_query($query);
while($row = mysql_fetch_assoc($result)) {
$price = $row['price'];
$code = $row['code'];
echo '<b>' . $code . '</b> = ' . $price . '<br>';
//mysql_query("UPDATE combined_stock SET price=$price WHERE partnumber='$code'") or die("There was a problem updating pricing columns: " . mysql_error());
}
echo '</div> Pricing column was successfully created.<br><br>';
You can do it all very nicely with a single SQL statement: 您可以使用一个SQL语句很好地完成所有操作:
UPDATE combined_stock dest LEFT JOIN (
SELECT price, code FROM temp_yahooprices
) src ON dest.partnumber = src.code
SET dest.price = src.price
This creates a temporary table called src
based on the data from temp_yahooprices. 这将基于temp_yahooprices中的数据创建一个称为
src
的临时表。 Then it goes through your target table (combined_stock) and updates all elements where the target partnumber
equals the source code
and updates its price. 然后,它将遍历目标表(combined_stock),并更新目标
partnumber
等于源code
所有元素,并更新其价格。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.