SitePoint Sponsor |
|
User Tag List
Results 1 to 4 of 4
Thread: Best Connection practices?
-
Apr 4, 2007, 11:57 #1
- Join Date
- Apr 2007
- Posts
- 2
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Best Connection practices?
Hello,
I have a very busy gaming website that is fully database driven. The site receives upto 15k uniques a day and 60 million page views per month.
Currently, each new page is included into the main index.php file. The connection to the database is achieved via the following in the index.php:
Code:include("config.php");
Code:$dbserver = "localhost"; $dbuser = "userhere"; $dbpass = "passhere"; $db = "dbnamehere"; mysql_connect($dbserver, $dbuser, $dbpass); mysql_select_db($db);
User visits site which loads index.php. User then goes to the games page which includes games.php into the index.php file in the appropriate place. So index.php is thus loaded twice. Once with the standard data and then once with the new included file.
There is no code anywhere in any script that close the db connection.
So my question to you is - is there a problem with this setup? And if so, what would be the best way to do this.
Thanks.
-
Apr 5, 2007, 07:36 #2
- Join Date
- Aug 2000
- Location
- Houston, TX, USA
- Posts
- 6,455
- Mentioned
- 11 Post(s)
- Tagged
- 0 Thread(s)
Well, for one thing, I would definitely check the connection with some sort of error checking. If the database is down you don't want the user to do anything really.
I have also requested for this thread to be moved to the PHP forum since it is more of a PHP conventions question.ssegraves [at] gmail.com
On Image Use, Abuse, and Where We're Headed
stephan | XMLHttpRequest Basics
flickr | last.fm | Cogentas, LLC
-
Apr 9, 2007, 15:12 #3
- Join Date
- Apr 2007
- Posts
- 2
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Anyone else have any input on this?
-
Apr 9, 2007, 23:05 #4
- Join Date
- Dec 2005
- Posts
- 53
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Well to start with I wouldn't put my code to connect to the database inside a file called config.php.
I am not sure by the way you explained it if config.php is included twice within the same request or across different requests. But doesn't really matter because by default mysql_connect will reuse the existing connection so you won't get two connections. But you probably want to use require_once() instead of include().
You don't need to explicitly call mysql_close() because the connection will be closed when the script ends anyway.
Another thing is you really should put the config.php file outside of the web root otherwise if php stops working for whatever reason the contents of any php file could be displayed in plain text.
Also you might want to consider using persistent connections.
http://www.php.net/manual/en/feature...onnections.php
http://www.php.net/manual/en/functio...l-pconnect.php
There are some things to be carefull of when using persistent connections so read up.
Bookmarks