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

mysql_connect() used twice

P: n/a
I need that my script retrieves data from two different databases so I use
mysql_connect() to open two connections. I have only one server, user and
password, just two different databases. Simplified, it'd be:
$con1=mysql_connect($server, $user, $pass);
mysql_select_db($database1, $con1);
$res1=mysql_query($sql1, $con1);

$con2=mysql_connect($server, $user, $pass);
mysql_select_db($database2, $con2);
$res2=mysql_query($sql2, $con2);

print_r($con1);
print_4($con2);

do_things_with_res1();
do_things_with_res2();

mysql_close($con2);
mysql_close($con1);
The problem is that, no matter what names I give to links and results, each
mysql_connect() overrides the previous ones. So when I call
do_things_with_res1() it actually uses $database2. The print_r() part shows
that $con1 has been unset!
I browsed php.ini and found this:

; Maximum number of links (persistent + non-persistent).
; -1 means no limit.
mysql.max_links = -1
I can't use only one connection because I have two different components
that are not necessarily called every time. I'm getting nuts so I'd really
appreciate any hint. Thank you in advance,
--
-- Álvaro G. Vicario - Burgos, Spain
-- Questions sent to my mailbox will be billed ;-)
--
Jul 17 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
*** Alvaro G Vicario wrote/escribió (Tue, 31 Aug 2004 12:24:31 +0200):
I need that my script retrieves data from two different databases so I use
mysql_connect() to open two connections. I have only one server, user and
password, just two different databases.


Please ignore my previous post! I read the manual page too quickly:

"If a second call is made to mysql_connect() with the same arguments, no
new link will be established, but instead, the link identifier of the
already opened link will be returned. The new_link parameter modifies this
behavior and makes mysql_connect() always open a new link, even if
mysql_connect() was called before with the same parameters."

Since my development PHP version is too old (it's 4.1.2 and the new_link
parameter became available in PHP 4.2.0) I'll have to redesign all my
code... or create a second MySQL user :(
--
-- Álvaro G. Vicario - Burgos, Spain
-- Questions sent to my mailbox will be billed ;-)
--
Jul 17 '05 #2

P: n/a
Alvaro G Vicario <al******************@telecomputeronline.com> wrote:
[snip]
Since my development PHP version is too old (it's 4.1.2 and the new_link
parameter became available in PHP 4.2.0) I'll have to redesign all my
code... or create a second MySQL user :(


Or just do things sequential...

--

Daniel Tryba

Jul 17 '05 #3

P: n/a
*** Daniel Tryba wrote/escribió (Tue, 31 Aug 2004 11:28:55 +0000 (UTC)):
Since my development PHP version is too old (it's 4.1.2 and the new_link
parameter became available in PHP 4.2.0) I'll have to redesign all my
code... or create a second MySQL user :(


Or just do things sequential...


i.e., redesign my code ;-)
--
-- Álvaro G. Vicario - Burgos, Spain
-- Questions sent to my mailbox will be billed ;-)
--
Jul 17 '05 #4

P: n/a
You don't need to open another connection to access another database. Use
the mysql_select_db() function instead.

I have an application that switches between six databases, so I know it
works.

--
Tony Marston

http://www.tonymarston.net

"Alvaro G Vicario" <al******************@telecomputeronline.com> wrote in
message news:3z****************************@40tude.net...
I need that my script retrieves data from two different databases so I use
mysql_connect() to open two connections. I have only one server, user and
password, just two different databases. Simplified, it'd be:
$con1=mysql_connect($server, $user, $pass);
mysql_select_db($database1, $con1);
$res1=mysql_query($sql1, $con1);

$con2=mysql_connect($server, $user, $pass);
mysql_select_db($database2, $con2);
$res2=mysql_query($sql2, $con2);

print_r($con1);
print_4($con2);

do_things_with_res1();
do_things_with_res2();

mysql_close($con2);
mysql_close($con1);
The problem is that, no matter what names I give to links and results,
each
mysql_connect() overrides the previous ones. So when I call
do_things_with_res1() it actually uses $database2. The print_r() part
shows
that $con1 has been unset!
I browsed php.ini and found this:

; Maximum number of links (persistent + non-persistent).
; -1 means no limit.
mysql.max_links = -1
I can't use only one connection because I have two different components
that are not necessarily called every time. I'm getting nuts so I'd really
appreciate any hint. Thank you in advance,
--
-- Álvaro G. Vicario - Burgos, Spain
-- Questions sent to my mailbox will be billed ;-)
--

Jul 17 '05 #5

P: n/a
.oO(Alvaro G Vicario)
Since my development PHP version is too old (it's 4.1.2 and the new_link
parameter became available in PHP 4.2.0) I'll have to redesign all my
code... or create a second MySQL user :(


Why not update?

Micha
Jul 17 '05 #6

P: n/a
*** Michael Fesser wrote/escribió (Tue, 31 Aug 2004 22:54:02 +0200):
Since my development PHP version is too old (it's 4.1.2 and the new_link
parameter became available in PHP 4.2.0) I'll have to redesign all my
code... or create a second MySQL user :(


Why not update?


Under Linux, I've found it close to impossible. The dependency tree forces
me to upgrade tons of packages--given than I can find binary RPMs for my
distro. If I compile from tarballs it's even worse because I can't solve
all dependencies in a single command line.

Of course I could try to upgrade the whole distro but that's too much
solution for such a small problem--I don't want to spend a week solving
secondary problems with web server or mail system.

--
-- Álvaro G. Vicario - Burgos, Spain
-- Questions sent to my mailbox will be billed ;-)
--
Jul 17 '05 #7

P: n/a
*** Tony Marston wrote/escribió (Tue, 31 Aug 2004 17:48:56 +0100):
You don't need to open another connection to access another database. Use
the mysql_select_db() function instead.


Manual says: "If no link is open, the function will try to establish a link
as if mysql_connect() was called without arguments, and use it."

That means that if the other module was loaded from current page it'll
connect correctly and if it isn't loaded it'll fail, doesn't it?

Funny thing, although the new_link parameter from mysql_connect() was
supposed to be introduced in PHP 4.2.0, it works just fine in my PHP 4.1.2.
That solved my problem :)
--
-- Álvaro G. Vicario - Burgos, Spain
-- Questions sent to my mailbox will be billed ;-)
--
Jul 17 '05 #8

P: n/a
.oO(Alvaro G Vicario)
*** Michael Fesser wrote/escribió (Tue, 31 Aug 2004 22:54:02 +0200):
Why not update?
Under Linux, I've found it close to impossible.


What distro?
The dependency tree forces
me to upgrade tons of packages--given than I can find binary RPMs for my
distro. If I compile from tarballs it's even worse because I can't solve
all dependencies in a single command line.
On a Debian system it's quite simple, the missing dependencies can be
installed quite easy (and most of them are resolved automatically). So
compiling PHP is more or less just a

../configure [--some-options]
make
make install

Sometimes the configure script complains about missing this or that, but
in most cases it's just a library and its header files, two packages to
install.

This reminds me of rebuilding PHP5 because I forgot the SimpleXML
extension the last time ... ;)
Of course I could try to upgrade the whole distro but that's too much
solution for such a small problem--I don't want to spend a week solving
secondary problems with web server or mail system.


4.1.2 is much too old (the recent one is 4.3.8), contains bugs and
security holes that are fixed meanwhile and lacks some newer features. I
wouldn't use it anymore.

Micha
Jul 17 '05 #9

P: n/a
*** Michael Fesser wrote/escribió (Wed, 01 Sep 2004 14:51:21 +0200):
What distro?
Actually two: the development box is a Red Hat 7.3 and the production one
is Red Hat 9. I don't really mind about security holes in 7.3, the machine
is not physically accesible from outside our LAN.
On a Debian system it's quite simple, the missing dependencies can be
installed quite easy (and most of them are resolved automatically). So
compiling PHP is more or less just a

./configure [--some-options]
make
make install


Sounds so cool... If only installing the distro itself wasn't such a pain
in the ass ;-)

--
-- Álvaro G. Vicario - Burgos, Spain
-- Questions sent to my mailbox will be billed ;-)
--
Jul 17 '05 #10

P: n/a
In article <cv****************************@40tude.net>, Alvaro G Vicario wrote:
*** Michael Fesser wrote/escribió (Tue, 31 Aug 2004 22:54:02 +0200):
Since my development PHP version is too old (it's 4.1.2 and the new_link
parameter became available in PHP 4.2.0) I'll have to redesign all my
code... or create a second MySQL user :(
Why not update?


Under Linux, I've found it close to impossible. The dependency tree forces
me to upgrade tons of packages--given than I can find binary RPMs for my
distro. If I compile from tarballs it's even worse because I can't solve
all dependencies in a single command line.


I know the problem! Grrrr!!! (And it's one of the reasons I dumped the RPM
based setups. RPM is wonderful for about 6 months until it's time to upgrade.

Debian was supposed to answer this problem by automatically keeping stuff up to
date, however if 1 link/file/directory is altered the whole thing comes
crashing down, which can make it difficult to install compiled stuff.
Of course I could try to upgrade the whole distro but that's too much
solution for such a small problem--I don't want to spend a week solving
secondary problems with web server or mail system.


If I ever get the time again, I plan on improving my toolkit for building linux
distrubutions from scratch. Seems all package based systems suffer from
similiar problems to varying degrees.

I use debian on one server these days and get quite frustrated with it's doing
stuff behind my back. The best I've found so far is LFS. (http://linuxfromscratch.com)

Unfortunately, it's a TON of work. Probably not best suited for a desktop system, but
WONDERFUL for LAMP based stuff.

Jamie
--
http://www.geniegate.com Custom web programming
User Management Solutions Perl / PHP / Java / UNIX

Jul 17 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.