By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,583 Members | 1,084 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,583 IT Pros & Developers. It's quick & easy.

Making mysql_connect more efficient in my script

P: n/a
LRW
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
Jul 17 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
LRW wrote:
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?


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

Jul 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.