April 30, 2012

using values from one mysql function for a second function

Question by rob melino

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

Answer by Hamurabi

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

Answer by Starx

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

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!