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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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