Page 1 of 1

Newbie question, PHP functions in seperate file

Posted: Sun Jun 19, 2005 8:10 pm
by Monty
Hello,


I'm making a bunch of functions like

GetAllUsers(...)
CreateNewUser(...)
GetUserDetails(...)
GetTopicname(...)
etc...

in an external php file. I call these functions from html files.
In the external file is also a function "MakeConnection(...)", that is called from EACH function that does database actions. The problem is that this will constantly open and close new database connections, for each small action that needs to be taken. Is there a way to open a db connection and keep it open ? Or is this not recommended ?
What happens if 20 users at the same time do database actions like creating new users? Will this open multiple DB connections at the same time ?

Posted: Sun Jun 19, 2005 8:13 pm
by Todd_Z
Lets take mySQL for example, you can get a resource handle from a connection:

Code: Select all

$hnd = mysql_connect( "localhost", "username", "password" );
mysql_select_db( "database", $hnd );
From there, you can use the $hnd in any function you want. I suggest that you do that at the very top of your first page, and anytime you are in a function, use the global keyword to access your database handle ($hnd).

*Edit* You don't need to keep track of the $hnd, only if you have multiple connections. If you are only using one connection, mysql automatically holds the resource for you. Check out http://www.php.net/mysql_connect for more.

Posted: Sun Jun 19, 2005 8:30 pm
by Monty
Todd_Z wrote:Lets take mySQL for example, you can get a resource handle from a connection:

Code: Select all

$hnd = mysql_connect( "localhost", "username", "password" );
mysql_select_db( "database", $hnd );
From there, you can use the $hnd in any function you want. I suggest that you do that at the very top of your first page, and anytime you are in a function, use the global keyword to access your database handle ($hnd).

*Edit* You don't need to keep track of the $hnd, only if you have multiple connections. If you are only using one connection, mysql automatically holds the resource for you. Check out http://www.php.net/mysql_connect for more.
OK thanx
But how do I know when to close the connection ? Is this when user closes it's browser ?
If more users are on the site at the same time, does this mean each user has it's own connection handle ? Or is it one handle globally, independent of the users that surf the site ?
I'm a bit confused.

Posted: Sun Jun 19, 2005 10:09 pm
by Todd_Z
Each user has their own handle to the database. Its not like a file lock, where only one handle can be opened on a file at one time, hundreds, thousands (I don't know the maximum) of handles can be querying a mySQL database at any given moment. You can close the mysql handle, mysql_close(), but you don't have to, as it closing automatically at the end of the script.

Posted: Sun Jun 19, 2005 10:35 pm
by Monty
Todd_Z wrote:Each user has their own handle to the database. Its not like a file lock, where only one handle can be opened on a file at one time, hundreds, thousands (I don't know the maximum) of handles can be querying a mySQL database at any given moment. You can close the mysql handle, mysql_close(), but you don't have to, as it closing automatically at the end of the script.
Ok, i understand now.
Thanx for your quick reply.

Monty