Jump to content

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
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;
}
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.