frequently ask ? : Computers : Programming : Languages : PHP : Database Backed Sites : MySQL

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

1 of 1 people (100%) answered Yes

Entry

For a high traffic database site, am I making a mistake to use mysql_connect throughout many function calls, or should it be passed as a parameter?

Jan 6th, 2003 17:48
Jean-Marc Molina, Kim Cosgrove,


mysql_connect "Open a connection to a MySQL Server" and "Returns a 
MySQL link identifier on success, or FALSE on failure" (PHP Manual).
You mean that you pass the link identifier to many functions ? It's 
inappropriate because you don't have to. You probably use 
mysql_db_query instead of mysql_query. The solution is to 
connect to MySQL (mysql_connect), to select your database with a 
mysql_select_db call and 
to use mysql_query.
Example:
$MySQLLinkId = @mysql_connect () or die ("can't Open a connection to a 
MySQL Server");
@mysql_select_db ("MyDatabase") or die ("can't Select a MySQL 
database");
$Records = @mysql_query ("SELECT * FROM MyTable") or die ("can't select 
records from MyTable");
The $MySQLLinkId is only useful to close the connection, and you don't 
have to (it's done when the script ends). Only use the link identifier 
if you use many databases, if you only have 1 database, you don't need 
it.