Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

multiple mysql_connect

Status
Not open for further replies.

ThomasJSmart

Programmer
Sep 16, 2002
634
trying to get multiple DB connections working but not having much luck, i tried the logic approach but its not doing the trick.

i have got 2 databases that need to be opened simultaneously:
1. a general database
2. a language specific database


general database connection script:
Code:
<?
$gdb = mysql_connect("localhost", "user","pasw");
mysql_select_db("general",$gdb);
?>

language specific database connection script:
Code:
<?
$ldb = mysql_connect("localhost", "user","pasw");
mysql_select_db("english",$ldb);
?>

then some code to make the connection and test it:

Code:
<?
include ("englishdb.php");
include ("generaldb.php");	

$genDB = mysql_query("SELECT info from sometable WHERE id='1'",$gdb);
while ($thisrow = mysql_fetch_array($genDB)) { echo "TEST1: ".$thisrow['info']."<br>";}

$lanDB = mysql_query("SELECT info from sometable WHERE id='1'",$ldb);
while ($thisrow = mysql_fetch_array($lanDB)) { echo "TEST2: ".$thisrow['info']."<br>";}
?>

the error im getting:
TEST1: bla
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in test.php on line 10

what i would like to see:
TEST1: bla
TEST2: bla

also note: i know i could do this using mysql_close and then reopening the database that i need for every query but im trying to open the databases at the top of the page(header.php), do all the queries in whatever database i need wherever i need them in the page then close the datbase connections at the bottom of the page (footer.php)

Any help much apreciated...
Thanks
Thomas


I learned a bit yesterday, today i learned a lot, imagine what i'll learn tomorrow!
 
That error is not indicative of a problem opening two MySQL connections at once. To test for that, compare the return from mysql_connect to FALSE or do something like:

$link = mysql_connect(....) [blue]or die(mysql_error())[/blue];


The error you're getting is indicative of a problem in this line:

$lanDB = mysql_query("SELECT info from sometable WHERE id='1'",$ldb);

your query has an error so mysql_query() returns FALSE. When you try to use mysql_fetch_array() against FALSE, you get that error. FALSE is not a valid result resource value.

As a first step, I recommend you check your query.


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Nevermind i found it....

hehe, well usufull for anybody else i suppose
this is the solution:

Code:
<?
$ldb = mysql_connect("localhost", "user","pasw",1);
mysql_select_db("english",$ldb);
?>

notice the ",1" at the end of the mysql_connect line :)

why cant i give myself a star??


I learned a bit yesterday, today i learned a lot, imagine what i'll learn tomorrow!
 
Yeah, that would do it.

PHP decides whether to open a new MySQL connection or reuse an old one based on the credentials handed to mysql_connect(). If here is an existing connection and you hand mysql_connect() the same user credentials as the credentials used to open the first connection, PHP will just reuse that connection.



You don't need two connections. MySQL supports the use of database names in queries:

SELECT * FROM databasename.tablename WHERE....


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
that works too :) cool thanks!

I learned a bit yesterday, today i learned a lot, imagine what i'll learn tomorrow!
 
One thing to keep in mind, whether you use one connection or multiple connections: The script we are discussing in this thread has nowhere near enough debugging code in it. Your code should have caught that the second connection resource was not being set.



Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top