Jump to content


Photo

calling $var from include() file


  • Please log in to reply
3 replies to this topic

#1 micah1701

micah1701
  • Members
  • PipPipPip
  • Advanced Member
  • 613 posts
  • LocationEllington, CT USA

Posted 22 May 2006 - 03:23 PM

I keep a config.inc file for my application which includes a var called $tablename which holds the string value of, you guessed it, the table name used throughout the applications.

on each page, I include() the config file and can call the $tablename var in my queries.
example:

[!--coloro:#FF0000--][span style=\"color:#FF0000\"][!--/coloro--]mysql_query("SELECT * FROM $tablename");[!--colorc--][/span][!--/colorc--]

This is working on all of my pages as it should; HOWEVER, it is failing when I include another page (called "functions.php") and try to run a function off that page.
example:

functions.php page has this function:
[!--coloro:#FF6600--][span style=\"color:#FF6600\"][!--/coloro--]function runQuery{
$result = mysql_query("SELECT * FROM $tablename");
return $result;
}[!--colorc--][/span][!--/colorc--]

index.php has this code:
[!--coloro:#33CC00--][span style=\"color:#33CC00\"][!--/coloro--]include('config.inc');
include('function.php');
$result = runQuery();[!--colorc--][/span][!--/colorc--]

I get no result because the query in the function does not recognize the tablename var which is included from the previously loaded config file.

what am I not understanding here?
Thanks for your thoughts!


"Confidence in the face of risk."

#2 wisewood

wisewood
  • Members
  • PipPipPip
  • Advanced Member
  • 226 posts
  • LocationRotherham, England

Posted 22 May 2006 - 03:33 PM

You need to pass the $tablename variable into your function.

This should work.


function runQuery($the_tablename)
{
$result = mysql_query("SELECT * FROM $the_tablename");
return $result;
}


$result = runQuery($tablename);

alternatively, it could just need this;

function runQuery()
{
$result = mysql_query("SELECT * FROM $tablename");
return $result;
}

wisewood: proven fact, I am both wise, and wooden.

#3 micah1701

micah1701
  • Members
  • PipPipPip
  • Advanced Member
  • 613 posts
  • LocationEllington, CT USA

Posted 22 May 2006 - 03:39 PM

That solved it! Thanks wisewood. you ARE very wise.

I had just assumed that the variable would be available to the function.

Thanks again!
"Confidence in the face of risk."

#4 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 22 May 2006 - 08:11 PM

or what you could do instead is inside your function declare it as a global variable, instead of passing it as an argument:

function runQuery()
{
global $tablename;

$result = mysql_query("SELECT * FROM $tablename");
return $result;
}

Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users