Can't get two open connections to work :S

Hello guys, I hope you can help.

I can’t get my page to switch between two database connections properly :frowning:
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]

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

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”

FOUND IT!

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

Only the value should be quoted apparently.

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

Yeah I always thought they were just the normal ’

Sponsor our Newsletter | Privacy Policy | Terms of Service