473,403 Members | 2,270 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

mysql_connect() used twice

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
10 5088
*** 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
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
*** 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
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
.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
*** 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
*** 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
.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
*** 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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: LRW | last post by:
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 =...
4
by: Andrew Clark | last post by:
Hello, I am having trouble connecting to my server with mysql_connect(). I can connect via the command line on the server and with phpMyAdmin over our network, but not though mysql_connect()....
2
by: Sugapablo | last post by:
I have a small test script connecting to a MySQL database. It seems to work, unless I try to use the resource link identifier returned by mysql_connect(); This works and returns all the rows in...
19
by: Michael | last post by:
Hi, I'm trying to do something which should be very simple - connect to the MySQL database. Here is the call, followed by the error msg. $conn = mysql_connect("localhost", "root", ""); ...
7
by: avenpace | last post by:
Hi, I got error when using mysql_connect function in my php script. If i set the db host to localhost, it give me that error altough all the user and password that I wrote is true(I can login...
6
by: GD | last post by:
Hi All, I've got MySQL 5.0.21 running on Windows Server 2003, and php running on Apache on a Linux box (Fedora Core 4). Previously when the pages were running on an IIS server the connection...
1
by: hugo | last post by:
Hello people, There is a problem is that mysql_connect() somehow caches last sessions IP and is not using the one which you put into host place. Has anyone made mysql_connect() from php to...
7
by: criveraf | last post by:
Hi there, I wasn't sure where to put this question, since it deals with both PHP and MySQL. I apologize if this is not the correct forum for this. I am working with a simple PHP application...
2
by: Ralf Seliger | last post by:
Hi, I hope someone here has a clue as to what is going on since I am completely baffled by the following: I'm working with a PHP/MySQL-web application called Moodle. When this application is...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.