Connecting Tech Pros Worldwide Help | Site Map

Making mysql_connect more efficient in my script

LRW
Guest
 
Posts: n/a
#1: Jul 17 '05
I'm working on some scripts someone else made, and it's littered with
repetitive mysql_connect's and mysql_select_db's.
For example:
// MAKE SURE IT IS A VALID PRODUCT
$connection2 = mysql_connect("localhost", "root") or die ("Couldn't
connect to the server.");
$db2 = mysql_select_db("priceretail", $connection2) or die ("Couldn't
select database.");
$sql2 = "SELECT dbname FROM productlist WHERE ((accountid =
'printingautomation') OR (accountid = '$accountid')) AND dbname =
'$sel_typeproduct'";

--SNIP--

// MAKE SURE IT IS A PRODUCT THAT HAS PRICING SET FOR IT
$connection3 = mysql_connect("localhost", "root") or die ("Couldn't
connect to the server.");
$db3 = mysql_select_db("priceretail", $connection3) or die ("Couldn't
select database.");
$sql3 = "SELECT companyid FROM $sel_typeproduct WHERE companyid
--SNIP--

Yes, it DOES say root in the script.
Here's what I'm wanting to do: make a script that contains the two
mysql_connect and mysql_select_db lines and then do a require() or
require_once() to call it. (And set up and use a different account
than root.)

Now, the questions:
Should I use require or require_once?
And is there a reason why the connection is established multiple times
that I don't understand? Can't it be established once at the top and
use new mysql_query's each time?

Thanks for any help!!
Liam
Marcin Dobrucki
Guest
 
Posts: n/a
#2: Jul 17 '05

re: Making mysql_connect more efficient in my script


LRW wrote:
[color=blue]
> And is there a reason why the connection is established multiple times
> that I don't understand? Can't it be established once at the top and
> use new mysql_query's each time?[/color]

Once you open a connection to MySQL, it will presist until the script
terminates, so all you should need to do is to open it once, and then
just do mysql_query on that connection.

If you look at the mysql function docs on the php.net site, you will
also notice that somewhere they talk about creating presistant
connections that last between scripts. It's useful when you have loads
of scripts and you are getting a serious overhead from the connection
being opened every time a script is run.

/Marcin

Closed Thread