0

What's the PHP code that would do one thing first and then take that and do a second.

1st: I want $_GET a variable and then run a query on it and return variables.

AND THEN

2nd: Use those "other" variables in a query.

I'm thinking I want to do this with two functions b/c my query is based off of what values I send from a form. So it has to get the values from the url and then run the query.

1
  • 1
    Please be a little bit more precise. Commented Apr 26, 2009 at 8:40

2 Answers 2

4
$variables = $_GET['variable'];

mysql_query("SELECT * FROM `table` WHERE `field` = '".mysql_real_escape_string($variables)."' LIMIT 20");

Do you want something like the above?

Sign up to request clarification or add additional context in comments.

4 Comments

I'm thinking I want to do this with two functions b/c my query is based off of what values I send from a form. So it has to get the values from the url and then run the query.
whoops! let me try. Thanks guys!
he means that he wants to write his own methods for these tasks!
Its probably not worth writing a function for something as simple as the above. If you needed to repeat it in the same way hundreds of times maybe.
1

yes, generally you can call any method (= function) from within any other method.

function getVars($vars)
{
    foreach ($vars as $key => $value)
    {
        doSomethingWithMyVars($key, $value)
    }
}

function doSomethingWithMyVars($key, $value)
{
    $sql = 'SELECT this, that FROM mytable WHERE '.$key.' = '.$value;
    //get data
}

getVars($_GET);

but attention, this is just exemplary code, you probably won't do it like that. Also the query wouldn't work for strings. it's just an example of how to call functions from within functions based on what your task seems to be more or less.

Comments

Your Answer

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