russthebarber Posted November 11, 2010 Share Posted November 11, 2010 Hi, I have just started using PHP again after a long break and am very rusty. I am having a few problems with some basic things: I have defined a function to connect to my MySQL database and it doesn't work. My $hostname, $username, $password variables are stored in a separate php file which I am including first and the standard mysql_connect function works ok, but I wanted to put it inside another user defined function called "condb" to make things quicker later. my function looks like this: function condb(){ mysql_connect($hostname, $username, $password) OR DIE ('Unable to connect to database! Please try again later.'); } and I am trying to run it like this: condb(); Unfortunately it is not working. Probably a basic error but any help appreciated. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/218403-basic-function-problem-connect-to-mysql/ Share on other sites More sharing options...
PFMaBiSmAd Posted November 11, 2010 Share Posted November 11, 2010 Functions, by definition, have their own isolated variable scope so that you can write whatever code you need in them to perform the desired function without any interference with the programs you use those functions in. The variables $hostname, $username, and $password don't exist inside your function unless you pass them in as parameters when you call the function or you define them inside the function. Quote Link to comment https://forums.phpfreaks.com/topic/218403-basic-function-problem-connect-to-mysql/#findComment-1133074 Share on other sites More sharing options...
russthebarber Posted November 11, 2010 Author Share Posted November 11, 2010 thanks. That makes sense. Quote Link to comment https://forums.phpfreaks.com/topic/218403-basic-function-problem-connect-to-mysql/#findComment-1133088 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.