I want to create a function to perform a redundant db query. What I thought would work is:

<?php
function my_function_one(){

global $a;

if (function_exists('my_function_two')){
       my_function_two($a);
        }
// view the value of the variable
echo 'The value is: ' $a;
}

function my_function_two($a){

$a = "SELECT * FROM {db_table} WHERE 1";
$rs = db_query($a);

return $a;
}
?>

I get 'The value is: ', but nothing else.

Add yourself to the long, distinguished, and growing list of people rescuing me by pointing me in the right direction here. Thank you in advance.

Comments

pceric’s picture

Something like this would probably be better:

$db = mysql_connect(); // or whatever

function my_function_one($db){
  $a = "SELECT * FROM {db_table} WHERE 1";
  $rs = $db->mysql_query($a);
  return mysql_fetch_row($rs);
}

$my_array = my_function_one($db);
echo 'The value is: ' . $my_array[0];

or inside another function:

function my_function_two($db){
  $my_array = my_function_one($db);
  echo 'The value is: ' . $my_array[0];
}

my_function_two($db);
mpruitt’s picture

I've really simplified the code in my example for my_function_two. There is alot going on (about 75 lines of code).

To avoid having to duplicate that code in every place that uses it, I just want to pass something like a user id to the second function, have that extract some data from a table, format it, and then pass the prepared data back to the function (my_function_one) that called it.

Once that prepared value is sent back to the mother function, more processing will take place.

I'm just having problems returning the processed value from the second function to the first.