phpnewby1918 Posted January 28, 2011 Share Posted January 28, 2011 Hi, Just a quick query. I call my db connection and configuration files on every script. I also have just made a functions script where i'm storing some functions except when i call the function, my script fails because of a missing database connection. But on the page that fails, i call the configuration files including the db connection at the start of the script. But when i get to where i include the function they are all on the same script so should it fail?? The page did work fine when the function was inbedded in the script. But as i'm using the function on a few pages, i thought it would be better practise and more efficent to just include the function on each page i need, rather than just copy and paste all the php in the required pages. Or do i have to declare the db connection inside the function page as well??? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/225994-including-functions-db-connections/ Share on other sites More sharing options...
l4nc3r Posted January 28, 2011 Share Posted January 28, 2011 What's the exact error? Quote Link to comment https://forums.phpfreaks.com/topic/225994-including-functions-db-connections/#findComment-1166687 Share on other sites More sharing options...
phpnewby1918 Posted January 28, 2011 Author Share Posted January 28, 2011 That my database variable isnt defined. But it is right at the start of the script Quote Link to comment https://forums.phpfreaks.com/topic/225994-including-functions-db-connections/#findComment-1166694 Share on other sites More sharing options...
l4nc3r Posted January 28, 2011 Share Posted January 28, 2011 The database variable is not in the scope of the function--it needs to be. Google php scope for more info on that. So you either have to make the database variable global, or use dependency-injection (meaning, pass the database variable as a parameter to each function). The latter is more pro. Quote Link to comment https://forums.phpfreaks.com/topic/225994-including-functions-db-connections/#findComment-1166695 Share on other sites More sharing options...
phpnewby1918 Posted January 28, 2011 Author Share Posted January 28, 2011 Thanks for your help, i'll look into it Appreciate the response Quote Link to comment https://forums.phpfreaks.com/topic/225994-including-functions-db-connections/#findComment-1166701 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.