micah1701 Posted December 11, 2009 Share Posted December 11, 2009 I'm new to frameworks and the whole model view control thing so please go easy on me... I have a page that is included() inside a view. In this page, I have a function() that check needs to do a mysql query that needs to know the database name which I keep in a variable. Apparently though, I can't just declare it as a variable any more? <?php $db = "database"; function sql(){ global $db; return "database name is: ".$db." ta-da!"; } echo sql();// returns: database name is: ta-da! why is this? Quote Link to comment https://forums.phpfreaks.com/topic/184801-no-global-vars-in-function-using-cake/ Share on other sites More sharing options...
premiso Posted December 11, 2009 Share Posted December 11, 2009 <?php global $db; $db = "database"; function sql(){ global $db; return "database name is: ".$db." ta-da!"; } echo sql();// returns: database name is: ta-da! You need to declare it as a DB outside the function as well. You may want to look into defineing the variable as a CONSTANT instead. Quote Link to comment https://forums.phpfreaks.com/topic/184801-no-global-vars-in-function-using-cake/#findComment-975617 Share on other sites More sharing options...
jcombs_31 Posted December 12, 2009 Share Posted December 12, 2009 Also doesn't sound like you are using the MVC structure correctly. You should just set the variable in your controller function to make it available to the view. Quote Link to comment https://forums.phpfreaks.com/topic/184801-no-global-vars-in-function-using-cake/#findComment-975999 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.