tags:

views:

527

answers:

2

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.

+4  A: 
$variables = $_GET['variable'];

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

Do you want something like the above?

Sam152
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.
that's what example does.
SilentGhost
whoops! let me try. Thanks guys!
he means that he wants to write his own methods for these tasks!
tharkun
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.
Sam152
+1  A: 

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.

tharkun
I worded it wrong at first. Please advise. Thanks!
Also you should use ' when using strings that shouldn't be interpreted an escape strings used in SQL queries, especially when someone inexperienced will use your code.
tstenner
@tstenner: edited.
tharkun