Jump to content

basic function problem - connect to mysql


russthebarber

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/218403-basic-function-problem-connect-to-mysql/
Share on other sites

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.

Archived

This topic is now archived and is closed to further replies.

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