call function within function

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
medc
Forum Newbie
Posts: 6
Joined: Mon Mar 02, 2009 3:56 pm

call function within function

Post by medc »

I have the following file structure:

file1 ----- contains some variables
file2 ----- contains functions A and B that use variables from file 1

function B in file2 needs to get the result of function A in file2
both functions A and B have a "global" declaration in the 1st line for the common variables that each of them need from file1

my problem is that when i run function B, i get an error from function A saying that it can't use the global variables from file1

if a place the global declaration in function B AFTER calling function A, function A executes OK but then function B gives the same error as before mentioned.

what is the problem here? how can i get around these problems with global variables??

i hope this isn't too confusing to understand. i tried to simplify the best i could

thanks in advance for your help!

PS if you need to see the actual code let me know and i'll post it.
sandstorm140
Forum Newbie
Posts: 10
Joined: Mon Mar 02, 2009 6:17 am

Re: call function within function

Post by sandstorm140 »

could ya plz post the code, would help understand much more.
medc
Forum Newbie
Posts: 6
Joined: Mon Mar 02, 2009 3:56 pm

Re: call function within function

Post by medc »

ok. here it is:

----------------------------------------------------------------------------
contents of file1 (stocks.php)

Code: Select all

<?php
$hostname_stocks = "localhost";
$database_stocks = "in";
$username_stocks = "root";
$password_stocks = "";
$stocks = mysql_pconnect($hostname_stocks, $username_stocks, $password_stocks) or trigger_error(mysql_error(),E_USER_ERROR); 
?>
-----------------------------------------------------------------------------
contents of file2:

Code: Select all

<?php require_once('Connections/stocks.php'); ?>
<?php
 
function MA($ticker, $date, $period) {
 
    global $database_stocks, $stocks;
    $result = 0;
 
    mysql_select_db($database_stocks, $stocks);
    $query_stocks = "SELECT * FROM stocks WHERE ticker =  '$ticker' AND `date` < '$date' ORDER BY `date` DESC LIMIT $period";
    $stocks = mysql_query($query_stocks, $stocks) or die(mysql_error());
    $row_stocks = mysql_fetch_assoc($stocks);
    $totalRows_stocks = mysql_num_rows($stocks);
 
    do {
        $result = $row_stocks['close'] + $result;
        echo $row_stocks['ticker'] . " ------- " . $row_stocks['date'] . " ------- " . $row_stocks['close'] . "<br>";
    } while ($row_stocks = mysql_fetch_assoc($stocks));
 
    echo "MA ----------------------------- " . number_format(($result/$period), 2) . "<br><br>";
    return number_format(($result/$period), 2);
 
mysql_free_result($stocks);
 
}
 
function EMA($ticker, $date, $period) {
 
    global $database_stocks, $stocks;
 
    mysql_select_db($database_stocks, $stocks);
    $query_stocks = "SELECT * FROM stocks WHERE ticker =  '$ticker' AND `date` < '$date' ORDER BY `date` ASC LIMIT 3";
    $stocks = mysql_query($query_stocks, $stocks) or die(mysql_error());
    $row_stocks = mysql_fetch_assoc($stocks);
    $totalRows_stocks = mysql_num_rows($stocks);
 
    do {
        $close = $row_stocks['close'];
 
        if(!isset($prevEMA)){
 
            $firstMA = MA($ticker, $date, $period);
            $result = (($close - $firstMA) * 2/($period + 1)) + $firstMA;
            $prevEMA = $result;
    
        } else {
            $result = (($close - $prevEMA) * (2/($period + 1))) + $prevEMA;
        }
    } while ($row_stocks = mysql_fetch_assoc($stocks));
 
    echo "EMA ----------------------------- " . $result . "<br>";
 
    mysql_free_result($stocks);
}
 
EMA("CBU", "2009-01-30", 9);
 
?>
 
-------------------------------------------------------------------------------

Notice the last line - EMA() is called. EMA() in turn calls MA()

no code is executed. the error i get is this:

Code: Select all

 
Warning: mysql_select_db(): supplied resource is not a valid MySQL-Link resource in C:\server\htdocs\invest\indicators.php on line 9
Warning: mysql_query(): supplied resource is not a valid MySQL-Link resource in C:\server\htdocs\invest\indicators.php on line 11
medc
Forum Newbie
Posts: 6
Joined: Mon Mar 02, 2009 3:56 pm

Re: call function within function

Post by medc »

does anyone know why my code doesn't work??
User avatar
susrisha
Forum Contributor
Posts: 439
Joined: Thu Aug 07, 2008 11:43 pm
Location: Hyderabad India

Re: call function within function

Post by susrisha »

i think its the problem with file location.

Code: Select all

 
include_once('Connections/stocks.php');
 
try this.
And at the start of the file, try to see if the variables are set by echoing
Post Reply