Jump to content
Old threads will finally start getting archived ×

calling $var from include() file


micah1701

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/10187-calling-var-from-include-file/
Share on other sites

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

This should work.


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


[code]
$result = runQuery($tablename);
[/code]

alternatively, it could just need this;

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.