我正在尝试查询我的数据库并创建值以供以后在另一个函数中使用。我的第一个函数(get_users())应查询请求数据库并查找为特定global_id列出的所有用户 - 此查询最多总共有4个用户。然后我想使用第二个函数(get_results())并将从第一个函数(get_users())检索到的值插入到第二个函数中。换句话说,我需要在第二个函数中将users1,2,3,4放入get_results($ user1,$ user2,$ user3,$ user4)。
希望有人可以提供帮助!这是我的职能:
function get_users($global_id)
{
$result = mysql_query("SELECT user_purchased FROM requests WHERE global_id = '$global_id'");
$row = mysql_fetch_row($result);
$user1 = $row[0];
$user2 = $row[0];
$user3 = $row[0];
$user4 = $row[0];
}
function get_results($user1, $user2, $user3, $user4)
{
$result = mysql_query("SELECT * FROM results WHERE username != '$user1'
AND username != '$user2'
AND username != '$user3'
AND username != '$user4'
ORDER BY distance");
...more stuff to do here with the query
}
由于
答案 0 :(得分:1)
在第一个函数中调用第二个函数:
function get_users($global_id)
{
$result = mysql_query("SELECT user_purchased FROM requests WHERE global_id = '$global_id'");
$count = 0;
while($row = mysql_fetch_array($result))
{
$user[$count] = $row;
$count++;
}
get_results($user[0],$user[1],$user[2],$user[3]);
}
function get_results($user1, $user2, $user3, $user4)
{
$result = mysql_query("SELECT * FROM results WHERE username != '$user1'
AND username != '$user2'
AND username != '$user3'
AND username != '$user4'
ORDER BY distance");
...more stuff to do here with the query
}
您甚至可以简化get_results函数,将一个变量作为数组而不是4个变量
function get_results($users)
{
$result = mysql_query("SELECT * FROM results WHERE username != '".$users[0]."'
AND username != '".$users[1]."'
AND username != '".$users[2]."'
AND username != '".$users[3]."'
ORDER BY distance");
...more stuff to do here with the query
}
你应该在第一个函数
中这样调用它get_results($users);
答案 1 :(得分:0)
将值作为参数发送到另一个函数。
function get_users($global_id)
{
$result = mysql_query("SELECT user_purchased FROM requests WHERE global_id = '$global_id'");
$row = mysql_fetch_row($result);
$user1 = $row[0];
$user2 = $row[0];
$user3 = $row[0];
$user4 = $row[0];
//now send
get_results($user1, $user2, $user3, $user4);
}