我试图用函数本身实际循环一个具有不同函数参数的函数;
我使用变量$login_name
作为函数参数,由表单提交。所以我想通过函数本身来改变这个参数。荒诞?还有其他选择吗?
这是我的功能
$login_name = "U00001";
while (!isset($end)) {
function getcount($login_name) {
$get_node = mysql_query("SELECT * FROM b_userbase WHERE login_name='$login_name'");
while ($row_node = mysql_fetch_array($get_node)) {
$node = $row_node["user_node"];
$placement = $row_node["user_placement"];
}
$current_count = mysql_query("SELECT * FROM f_user_matching WHERE u_m_mem='$placement'");
while ($get_count = mysql_fetch_array($current_count)) {
$get_left_current = $get_count["u_m_left_current"];
$get_left_total = $get_count["u_m_left_total"];
$get_right_current = $get_count["u_m_right_current"];
$get_right_total = $get_count["u_m_right_total"];
$update_left_current = $get_left_current + 1;
$update_right_current = $get_right_current + 1;
$update_left_total = $get_left_total + 1;
$update_right_total = $get_right_total + 1;
}
if ($node == "L") {
$increase_qry = mysql_query("UPDATE f_user_matching
SET u_m_left_current= '{$update_left_current}',
u_m_left_total ='{$update_left_total}'
WHERE u_m_mem ='{$placement}' ");
}
if ($node == "R") {
$increase_qry = mysql_query("UPDATE f_user_matching
SET u_m_right_current= '{$update_right_current}',
u_m_right_total ='{$update_right_total}'
WHERE u_m_mem ='{$placement}' ");
}
$login_name = $placement;
if ($login_name == "IM000001") {
$end = 1;
} else {
$login_name = $placement;
}
global $login_name;
}
$count = getcount($login_name);
}
问题出在哪里?
答案 0 :(得分:0)
我不明白你在做什么,所以你可能会尝试做一些我不知道的不同的事情,但如果你需要的是一个递归函数那么这不是那样的。你没有在while循环中定义函数,函数应该被定义为一个普通的函数,但是可以自己调用不同的结果。
这就是你的函数递归的方式。 这不是经过测试的代码
<?php
$login_name = "U00001";
$count = getcount($login_name);
function getcount($login_name) {
$get_node = mysql_query("SELECT * FROM b_userbase WHERE login_name='$login_name'");
while ($row_node = mysql_fetch_array($get_node)) {
$node = $row_node["user_node"];
$placement = $row_node["user_placement"];
}
$current_count = mysql_query("SELECT * FROM f_user_matching WHERE u_m_mem='$placement'");
while ($get_count = mysql_fetch_array($current_count)) {
$get_left_current = $get_count["u_m_left_current"];
$get_left_total = $get_count["u_m_left_total"];
$get_right_current = $get_count["u_m_right_current"];
$get_right_total = $get_count["u_m_right_total"];
$update_left_current = $get_left_current + 1;
$update_right_current = $get_right_current + 1;
$update_left_total = $get_left_total + 1;
$update_right_total = $get_right_total + 1;
}
if ($node == "L") {
$increase_qry = mysql_query("UPDATE f_user_matching
SET u_m_left_current= '{$update_left_current}',
u_m_left_total ='{$update_left_total}'
WHERE u_m_mem ='{$placement}' ");
}
if ($node == "R") {
$increase_qry = mysql_query("UPDATE f_user_matching
SET u_m_right_current= '{$update_right_current}',
u_m_right_total ='{$update_right_total}'
WHERE u_m_mem ='{$placement}' ");
}
$login_name = $placement;
if ($login_name != "IM000001") {
$login_name = $placement;
$count = getcount($login_name);
}
global $login_name; // Dont know why you want this one?
}
?>
不知道为什么你最后有* global $ login_name; *。您希望函数返回某些内容还是仅在数据库中执行 UPDATE ?