It does seem wasteful, but you have to make sure there is a connection before you query a database. What if the user goes out to lunch? Should you maintain an open database connection for an hour? No, that's a lot of overhead.
What you can do is change it from mysql_connect to mysql_pconnect. That will make it a persistent connection, which may be what you are looking for. It actually leaves the connection open after it's done using, which is just what I said you don't want to do. But what PHP does is reuse open connections to the database. You could have 30 users visiting your site, but only 5 active at any one time. PHP will then use only 5 database connections.
You are still making the connection call, but PHP will just say "OK, I already have one I can use and here is it's id".


On Friday, September 26, 2003, at 04:10 PM, Daevid Vincent wrote:

I've been coding with PHP and mySQL for years. And this part has never sat
right with me and finally I'm getting the nerve to ask how the pros out
there do this...


What I do is have a "db.php" file that contains:

$db = mysql_connect ("localhost","username","password")
                or die ("Could not connect to SQL server.");
mysql_select_db ("mydb",$db)
                or die ("Could not select Database");


Then at the top of every page, I require_once("db.php");


But it just seems so wasteful to keep making that mysql_connect().

Isn't there a way to store the $db result so I don't have to keep
connecting.
And how about the mysql_select_db() part too? Since I'm going to stay in the
same db most of the time.


I thought I read that I can't use a $_SESSION for it, and that's the only
way I know of to have it be available on each page?


--
Brent Baisley
Systems Architect
Landover Associates, Inc.
Search & Advisory Services for Advanced Technology Environments
p: 212.759.6400/800.759.0577

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Reply via email to