0

我正在尝试查询我的数据库并创建值以供以后在另一个函数中使用。我的第一个函数 (get_users()) 应该查询 requests 数据库并找到为特定 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
    }

谢谢

4

2 回答 2

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);
于 2012-04-30T23:49:37.760 回答
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);
}
于 2012-04-30T23:52:31.830 回答