# Can't get two open connections to work :S

**URL:** <https://www.phphelp.com/t/cant-get-two-open-connections-to-work-s/15033>\
**Category:** General Database (Oracle, SQL Server, etc)\
**Created:** [November 17, 2011, 8:32pm UTC](https://www.phphelp.com/t/cant-get-two-open-connections-to-work-s/15033 "2011-11-17T20:32:48Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mavrik347](https://www.phphelp.com/user_avatar/www.phphelp.com/mavrik347/32/500_2.png) [@Mavrik347](https://www.phphelp.com/u/Mavrik347)\
**Post date:** [November 17, 2011, 8:32pm UTC](https://www.phphelp.com/t/cant-get-two-open-connections-to-work-s/15033/1 "2011-11-17T20:32:48Z")

</div>

Hello guys, I hope you can help.

I can’t get my page to switch between two database connections properly ☹  
It seems to ignore the second connection.

I’m trying to go through each group in database one and find out how many members they have registered in database two by checking the field “tsName”.

This is a valid query for database one.  
[php]$fetchUsers = mysql\_query(“SELECT \* FROM $db\_corps\_table ORDER BY cName;”, $con1);[/php]  
This is a valid query for database two.  
[php]$fetchUsers = mysql\_query(“SELECT \* FROM ‘users’ WHERE tsName LIKE $cTicker%;”, $con2);[/php]  
However despite me specifying which database link to use it tries to use every query on database one ($con1). So I can never query database two ($con2) because it just breaks with the result below. :S

[php]$fetchUsers = mysql\_query(“SELECT \* FROM $db\_corps\_table ORDER BY cName;”, $con1);[/php] gives

> AREA 43 [A-43] (11): result result result (there are 3 groups in database one)

But: [php]$fetchUsers = mysql\_query("SELECT \* FROM 'users' WHERE tsName LIKE $cTicker%;", $con2); [/php] gives

> AREA 43 [A-43] (11):
> 
> Warning: mysql\_fetch\_array(): supplied argument is not a valid MySQL result resource in /home/sever/public\_html/mav/tools/ts\_corp\_registrations.php on line 51

[php]\<?php  
// ===== Connect to database one (group) =====  
$con1 = mysql\_connect($db\_host,$db\_corps\_user,$db\_corps\_pass);  
if (!$con1) {  
die(“Could not connect:” . mysql\_error());  
}  
$db\_select1 = mysql\_select\_db($db\_corps\_db, $con1);  
if (!$db\_select1) {  
die (“Could not select database:” . mysql\_error());  
}  
// ===== Connect to database two (members) =====  
$con2 = mysql\_connect($db\_host,$db\_ts\_user,$db\_ts\_pass, true);  
if (!$con2) {  
die(“Could not connect:” . mysql\_error());  
}  
$db\_select2 = mysql\_select\_db($db\_ts\_db, $con2);  
if (!$db\_select2) {  
die (“Could not select database:” . mysql\_error());  
}

$fetchCorps = mysql\_query(“SELECT \* FROM $db\_corps\_table ORDER BY cName;”, $con1);  
while ($row = mysql\_fetch\_array($fetchCorps)) {  
// For each of the results, gather details from database one, then see how many members we can find in database two  
$cName = $row[“cName”];  
$cTicker = mysql\_real\_escape\_string($row[“cTicker”],$con1);  
$cMembers = $row[“cMembers”];

```
echo $cName." [".$cTicker."] (".$cMembers."):<br />";
// ========================== HERE BE PROBLEMS ==========================
//$fetchUsers = mysql_query("SELECT * FROM $db_corps_table ORDER BY cName;", $con1);
$fetchUsers = mysql_query("SELECT * FROM 'users' WHERE tsName LIKE $cTicker%;", $con2);
// ===================================================================
while ($rowTS = mysql_fetch_array($fetchUsers)) {
	echo "result<br />";
}

```

}  
// all done, close DB connections  
mysql\_close($con2);  
mysql\_close($con1);  
?\>[/php]

---

<div class="post-metadata">

**Author:** ![RaythXC](https://www.phphelp.com/letter_avatar_proxy/v4/letter/r/d9b06d/32.png) [@RaythXC](https://www.phphelp.com/u/RaythXC)\
**Post date:** [November 17, 2011, 8:50pm UTC](https://www.phphelp.com/t/cant-get-two-open-connections-to-work-s/15033/2 "2011-11-17T20:50:52Z")

</div>

Why do you need more than 1 database? You could simply use multiple tables instead.

---

<div class="post-metadata">

**Author:** ![Mavrik347](https://www.phphelp.com/user_avatar/www.phphelp.com/mavrik347/32/500_2.png) [@Mavrik347](https://www.phphelp.com/u/Mavrik347)\
**Post date:** [November 17, 2011, 8:54pm UTC](https://www.phphelp.com/t/cant-get-two-open-connections-to-work-s/15033/3 "2011-11-17T20:54:07Z")

</div>

It’s a script that combines 2 apps data to calculate a % of registered members. So they are in different databases. It would be unwise to merge the website database with the teamspeak one. :S

I found out that the error is (I think) in the LIKE statement but I can’t find out how to get it to work… All seems right to me. :S

“You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘‘users’ WHERE tsName ‘LIKE%’ A-43’ at line 1”

---

<div class="post-metadata">

**Author:** ![Mavrik347](https://www.phphelp.com/user_avatar/www.phphelp.com/mavrik347/32/500_2.png) [@Mavrik347](https://www.phphelp.com/u/Mavrik347)\
**Post date:** [November 17, 2011, 9:00pm UTC](https://www.phphelp.com/t/cant-get-two-open-connections-to-work-s/15033/4 "2011-11-17T21:00:38Z")

</div>

FOUND IT!

The query should have been:  
SELECT \* FROM users WHERE tsName LIKE ‘$cTicker%’;

Only the value should be quoted apparently.

---

<div class="post-metadata">

**Author:** ![RaythXC](https://www.phphelp.com/letter_avatar_proxy/v4/letter/r/d9b06d/32.png) [@RaythXC](https://www.phphelp.com/u/RaythXC)\
**Post date:** [November 17, 2011, 9:05pm UTC](https://www.phphelp.com/t/cant-get-two-open-connections-to-work-s/15033/5 "2011-11-17T21:05:37Z")

</div>

Oh I see. If you want to put the quote things around like a field title you need to use `name` not ‘name’

---

<div class="post-metadata">

**Author:** ![Mavrik347](https://www.phphelp.com/user_avatar/www.phphelp.com/mavrik347/32/500_2.png) [@Mavrik347](https://www.phphelp.com/u/Mavrik347)\
**Post date:** [November 17, 2011, 9:07pm UTC](https://www.phphelp.com/t/cant-get-two-open-connections-to-work-s/15033/6 "2011-11-17T21:07:06Z")

</div>

Yeah I always thought they were just the normal ’
