0

I am trying to query my database and create values for later use in another function. My first function (get_users()) should query the requests database and find all users listed for the specific global_id - there will always be a maximum of 4 users for this query. Then I want to use a second function (get_results()) and insert the values that were retrieved from the first function (get_users()) into the second function. In other words, i need to put users1,2,3,4 into get_results($user1, $user2, $user3, $user4) in the second function.

Hoping someone can help! Here are my functions:

        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
    }

Thanks

3
  • It might be worth considering a JOIN: w3schools.com/sql/sql_join.asp Commented Apr 30, 2012 at 23:09
  • Is it good to repeat $row[0] when defining the $user1 to $user4? Commented Apr 30, 2012 at 23:13
  • yeah i think it might be $row[1],$row[2],$row[3],$row[4]? Commented Apr 30, 2012 at 23:40

2 Answers 2

1

Call the second function inside the first one:

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
    }

You can even simplify the get_results function to have one variable as an array instead of 4 varialbles

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
        }

And you should call it like this in the first function

get_results($users);
Sign up to request clarification or add additional context in comments.

Comments

0

Send the values as parameter to the another function.

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);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.