I can't seem to get the array on $sql_result_array
be available out of the function.
function function_name($query, $table_name){
global $connection;
$sql_query = " SELECT * FROM {$table_name} WHERE name = '{$query}' ";
$sql_result = mysql_query($sql_query, $connection);
$sql_result_array = mysql_fetch_array($sql_result);
}
function_name($value1, $value2);
print_r($sql_result_array); // (outputs) Undefined variable: sql_result_array
Any suggestion on how to get that array available outside the function?
You can either do
print_r(function_name($value1, $value2));
or
$sql_result_array = function_name($value1, $value2)
print_r($sql_result_array);
The general idea here, is that any variable you declare inside of a function is ONLY available inside of that function. So if you have
function a() {
$i = 5;
return $i;
}
OUTSIDE of that function, $i
does not exist. Or if it DOES exist, setting $i
to 5 INSIDE that function, won't change the value of $i
OUTSIDE of that function. This is good because otherwise, you would have to use unique variable names EVERYWHERE, otherwise you would never know if a value got changed somewhere else.
We return $i
so that is we want to use that value outside of the function, we can. But we have to either assign it
$other_variable = a();
or use it directly
do_something_else(a())
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments