Jump to content


Photo

When to maek a new connection() to database


  • Please log in to reply
2 replies to this topic

#1 arianhojat

arianhojat
  • Members
  • PipPipPip
  • Advanced Member
  • 235 posts

Posted 21 June 2006 - 01:06 PM

whenever i perform a query, i got into the habit of supplying the database and table by calling mysql_connect() everytime, just in case the table changes which it might or to be explicit when checking my code.

is this a performance hit, should it not be done unless table definately changes?

Example:

$host = "localhost"; $user = "user"; $pass = "pass";  $db = "clientsInfo";        
$query = "SELECT * FROM clientsInfo.clients";
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!"); 
mysql_select_db($db) or die ("Unable to select database!"); 
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());         

//...later

$host = "localhost"; $user = "user"; $pass = "pass";  $db = "clientsInfo";        
$query = "SELECT * FROM clientsInfo.clientAddresses";
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!"); 
mysql_select_db($db) or die ("Unable to select database!"); 
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());     


#2 poirot

poirot
  • Members
  • PipPipPip
  • Advanced Member
  • 646 posts
  • LocationAustin, TX

Posted 21 June 2006 - 01:21 PM

You call mysql_connect ONCE, unless you want to make different connections.

You call mysql_select_db ONCE, unless you want to use different databases.

If you are using the same connection, and the same database, it is definitely a waste of resources.
~ D Kuang

#3 arianhojat

arianhojat
  • Members
  • PipPipPip
  • Advanced Member
  • 235 posts

Posted 21 June 2006 - 02:01 PM

wow thanx, makes alot of sense. doh.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users