Jump to content

Archived

This topic is now archived and is closed to further replies.

arianhojat

When to maek a new connection() to database

Recommended Posts

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:

[code]$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());     [/code]

Share this post


Link to post
Share on other sites
You call mysql_connect ONCE, unless you want to make different [b]connections[/b].

You call mysql_select_db ONCE, unless you want to use different [b]databases[/b].

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

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.