Jax2 Posted April 6, 2010 Share Posted April 6, 2010 Hi all. On a page inside my includes directory, I have shoutbox.php. Shoutbox.php includes db.php which is in the same directory, hence: include("db.php"); Directly after that include statement, I can echo a variable ($prefix) that is stored inside of db.php, which correctly returns ts_ Now, further down in the code, I am trying to access a table in my database known as ts_shoutbox, so I am using ".$prefix."shoutbox which should work fine, yet I am getting the error: Table 'tsrecipe.shoutbox' doesn't exist which is correct, because it's supposed to be tsrecipe.ts_shoutbox, not .shoutbox Here is the code: <?php include("db.php"); echo "Prefix: ".$prefix."; function getContent($link, $num){ $res = @mysql_query("SELECT ShoutBoxID, date, user, message FROM ".$prefix."shoutbox WHERE ShoutBoxID='$recipeID' ORDER BY date DESC LIMIT ".$num, $link); if(!$res) die("Error: ".mysql_error()); else return $res; } I don't understand why it echos ts_ perfectly at the top but refuses to use it in the function. Any help would be appreciated. Link to comment https://forums.phpfreaks.com/topic/197729-need-help-with-broken-variable/ Share on other sites More sharing options...
Jax2 Posted April 6, 2010 Author Share Posted April 6, 2010 Found my problem.... I needed to define $prefix as a global variable inside of the function. It now works. function getContent($link, $num){ global $prefix; $res = @mysql_query("SELECT date, user, message FROM ".$prefix."shoutbox ORDER BY date DESC LIMIT ".$num, $link); if(!$res) die("Error: ".mysql_error()); else return $res; } Link to comment https://forums.phpfreaks.com/topic/197729-need-help-with-broken-variable/#findComment-1037683 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.