View Full Version : opening a database with mysql_select_db in php
jhewer
07-28-2004, 11:03 AM
hi
i'm new to php and mysql, and am currently creating a user logon system using a mysql table to hold the user account data. it all works fine, but now i'm just trying to improve my code a bit.
the question i have regards opening the database with mysql_select_db.
do i only need to open the database once while the browser is open, or do i need to open the database on every page?
if so, i have a header file which is 'required' by each page, could i use mysql_select_db here to open the database, or do i need to do it in each actual php file which uses the database?
cheers
jon
do i only need to open the database once while the browser is open, or do i need to open the database on every page?
The webserver is unawear if the browser is open or not. The connection to the db is also between the webserver and the dataserver. There is no direct connection between the browser and the dataserver (your applicationlayer (=PHP in your case) does this) and you could even use just a few open 'persitent' connections to server all clients.
On each page, where you need db-interaction, you need to open a connection or use an open, persistent, connection.
Without knowing your actual situation, i can't realy tell you 100% which of the two is best, but i'm 99.9% convinced that persistent connections wount improve performance for your site.
So just open a new connection on each page (they are automatically closed when the script ends)
if so, i have a header file which is 'required' by each page, could i use mysql_select_db here to open the database, or do i need to do it in each actual php file which uses the database?
Your connectionstring should definitely be in a seperate file that you then include (using require())
this makes it easier to move the app, to switch to a different db-server, to change the useraccount periodically etc
Your connectionfile should look like
<?php
$link = @mysql_connect('localhost', '**********', '*********')
or die('Could not connect to the databaseserver. Please go back and try again or try again later.');
@mysql_select_db('*********',$link)
or die ('Could not select database. Please go back and try again or try again later.');
?>
you include it like
require(./includes/dzedzeconnectionddqsd.php);
If you use more then one db inside a single app, then i'd recommend creating different connectionfiles for each db, and using differnet mysql-accounts for each db.
jhewer
07-28-2004, 09:15 PM
excellent, thanks for the advice
just one quick question about the php in ur code above, what is the @ before mysql_select_db for?
cheers
jon
excellent, thanks for the advice
just one quick question about the php in ur code above, what is the @ before mysql_select_db for?
cheers
jon
If you put a @ in front of a function, then the errormessage is suppressed.
For these 2 functions, you better suppress the errormessages since they can disclose info about the servername,usernamename and db-name.
the "or die()" will then make sure that your own erromessage is returned.
But in a real production-environment, you better suppress all errormessages altogether by setting the errolevel ar tuntime or inside your php.ini
jhewer
07-29-2004, 01:25 PM
ah right i see, cheers m8
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.