gpt4 book ai didi

php - 循环 "mysql_query"仅适用于少量循环?

转载 作者:搜寻专家 更新时间:2023-10-31 22:14:14 26 4
gpt4 key购买 nike

下面的代码是我用来更新 mySQL 数据库的 php 文件的一部分。

我可以在 FF Firebug 控制台中看到 POST 数据以 POST 到此 php 文件的形式都是正确的。

问题是,如果 $i 大于 3,我会得到以下错误:

The connection was reset
The connection to the server was reset while the page was loading."

这是否表示我必须编写自定义 php.ini?如果是这样,我需要增加哪些值?

我意识到我的循环 mysql_query 代码也可能不正确,但它确实适用于 1 到 3 的 $i 值。有人可以建议正确的编码,如果这是情况?

        for ($i=1; $i<=$rr_years; $i++){
$idrr = GetSQLValueString($_POST['id' . $i], "int");
$associated_horse = GetSQLValueString($_POST['associated_horse' . $i], "int");
$year = GetSQLValueString($_POST['year' . $i], "text");
$age = GetSQLValueString($_POST['age' . $i], "int");
$starts = GetSQLValueString($_POST['starts' . $i], "int");
$first = GetSQLValueString($_POST['first' . $i], "int");
$first_sw = GetSQLValueString($_POST['first_sw' . $i], "int");
$second = GetSQLValueString($_POST['second' . $i], "int");
$second_sp = GetSQLValueString($_POST['second_sp' . $i], "int");
$third = GetSQLValueString($_POST['third' . $i], "int");
$third_sp = GetSQLValueString($_POST['third_sp' . $i], "int");
$age_notes = GetSQLValueString($_POST['age_notes' . $i], "text");
$age_text = GetSQLValueString($_POST['age_text' . $i], "text");
$earned = GetSQLValueString($_POST['earned' . $i], "text");


mysql_select_db($database_HDAdave, $HDAdave);
mysql_query("UPDATE race_records SET
associated_horse = $associated_horse,
year = $year,
age = $age,
starts = $starts,
first = $first,
first_sw = $first_sw,
second = $second,
second_sp = $second_sp,
third = $third,
third_sp = $third_sp,
age_notes = $age_notes,
age_text = $age_text,
earned = $earned


WHERE rr_id = $idrr", $HDAdave) or die(mysql_error());

}

感谢您提供的任何帮助。

最佳答案

这更多的是个人品味,而不是真正的性能问题,但 GetSQLValueString()(对于那些想知道的人,它是在使用 Dreamweaver 的数据库帮助时生成的 Macromedia DW 自定义函数。在此 SO question 中显示和解释)有时是“太多”,一个简单的 mysql_real_escape_string() 或检查值是否为 INT 就足够了。

另外,我会将数据库的连接和选择放在循环之外。此外,但通常不需要,您可以在每次通过后释放 mysql 结果:

// connect to DB here.
mysql_select_db($database_HDAdave, $HDAdave);

for ($i=1; $i<=$rr_years; $i++){
$idrr = intval($_POST['id' . $i]);
$associated_horse = intval($_POST['associated_horse' . $i]);
$year = mysql_real_escape_string($_POST['year' . $i]);
$age = intval($_POST['age' . $i]);
$starts = intval($_POST['starts' . $i]);
$first = intval($_POST['first' . $i]);
$first_sw = intval($_POST['first_sw' . $i]);
$second = intval($_POST['second' . $i]);
$second_sp = intval($_POST['second_sp' . $i]);
$third = intval($_POST['third' . $i]);
$third_sp = intval($_POST['third_sp' . $i]);
$age_notes = mysql_real_escape_string($_POST['age_notes' . $i]);
$age_text = mysql_real_escape_string($_POST['age_text' . $i]);
$earned = mysql_real_escape_string($_POST['earned' . $i]);

mysql_query("UPDATE `race_records` SET
`associated_horse` = $associated_horse,
`year` = '$year',
`age` = $age,
`starts` = $starts,
`first` = $first,
`first_sw` = $first_sw,
`second` = $second,
`second_sp` = $second_sp,
`third` = $third,
`third_sp` = $third_sp,
`age_notes` = '$age_notes',
`age_text` = '$age_text',
`earned` = '$earned'
WHERE `rr_id` = $idrr", $HDAdave) or die(mysql_error());
// not needed:
mysql_free_result($HDAdave);
}
?>

关于php - 循环 "mysql_query"仅适用于少量循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8600089/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com