Mavrik347 Posted November 17, 2011 Share Posted November 17, 2011 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. $fetchUsers = mysql_query("SELECT * FROM $db_corps_table ORDER BY cName;", $con1); This is a valid query for database two. $fetchUsers = mysql_query("SELECT * FROM 'users' WHERE tsName LIKE $cTicker%;", $con2); 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 $fetchUsers = mysql_query("SELECT * FROM $db_corps_table ORDER BY cName;", $con1); gives AREA 43 [A-43] (11): result result result (there are 3 groups in database one) But: $fetchUsers = mysql_query("SELECT * FROM 'users' WHERE tsName LIKE $cTicker%;", $con2); 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 // ===== 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/251329-cant-get-two-open-sql-connections-to-work/ Share on other sites More sharing options...
ManiacDan Posted November 17, 2011 Share Posted November 17, 2011 $fetchUsers = mysql_query("SELECT * FROM 'users' WHERE tsName LIKE $cTicker%;", $con2); There's no quotes around your LIKE term and your tablename needs to be enclosed in `backticks` not 'single quotes'. Always echo the error and never assume you know what the problem is. It's probably using the right db, your query is malformed. Quote Link to comment https://forums.phpfreaks.com/topic/251329-cant-get-two-open-sql-connections-to-work/#findComment-1289039 Share on other sites More sharing options...
Mavrik347 Posted November 17, 2011 Author Share Posted November 17, 2011 Thanks for the fast reply! I tried both SELECT * FROM 'users' WHERE tsName 'LIKE $cTicker%'; SELECT * FROM 'users' WHERE tsName 'LIKE' $cTicker%; SELECT * FROM 'users' WHERE tsName LIKE% $cTicker; SELECT * FROM 'users' WHERE tsName 'LIKE%' $cTicker; SELECT * FROM 'users' WHERE tsName LIKE '$cTicker%'; But still the same error. :/ You're right though, it was very silly of me to not echo the mysql_error() "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" Quote Link to comment https://forums.phpfreaks.com/topic/251329-cant-get-two-open-sql-connections-to-work/#findComment-1289045 Share on other sites More sharing options...
xyph Posted November 17, 2011 Share Posted November 17, 2011 Those are all incorrect. Quote Link to comment https://forums.phpfreaks.com/topic/251329-cant-get-two-open-sql-connections-to-work/#findComment-1289047 Share on other sites More sharing options...
Mavrik347 Posted November 17, 2011 Author Share Posted November 17, 2011 lol care to throw me a bone? Quote Link to comment https://forums.phpfreaks.com/topic/251329-cant-get-two-open-sql-connections-to-work/#findComment-1289048 Share on other sites More sharing options...
xyph Posted November 17, 2011 Share Posted November 17, 2011 tsName is your 'variable' LIKE is your 'operator' $cTicker% is your 'value' Which one do you quote? Syntax is the first thing covered when learning any language. This is something you should know before you attempt to code anything. Quote Link to comment https://forums.phpfreaks.com/topic/251329-cant-get-two-open-sql-connections-to-work/#findComment-1289054 Share on other sites More sharing options...
Mavrik347 Posted November 17, 2011 Author Share Posted November 17, 2011 SELECT * FROM users WHERE tsName LIKE '$cTicker%'; Thanks man, so much. Had my head in a spin there for a minute. I generally know SQL syntax, the reason I was so confused is that the original query I typed into PHPMyAdmin worked straight off the bat. Then when I made it generate the query for me it generated the same thing I just typed. So I thought it must be right. All working as you can see. Thank you lovely chaps Quote Link to comment https://forums.phpfreaks.com/topic/251329-cant-get-two-open-sql-connections-to-work/#findComment-1289058 Share on other sites More sharing options...
ManiacDan Posted November 17, 2011 Share Posted November 17, 2011 If the original query worked, then the original query didn't have single quotes around your table name. Quote Link to comment https://forums.phpfreaks.com/topic/251329-cant-get-two-open-sql-connections-to-work/#findComment-1289073 Share on other sites More sharing options...
Mavrik347 Posted November 18, 2011 Author Share Posted November 18, 2011 This is the query PHPMyAdmin generated: SELECT * FROM `users` WHERE `tsName` LIKE '%IRNP%'; I was not aware however that there was any difference between a ` and a '. Quote Link to comment https://forums.phpfreaks.com/topic/251329-cant-get-two-open-sql-connections-to-work/#findComment-1289147 Share on other sites More sharing options...
xyph Posted November 18, 2011 Share Posted November 18, 2011 a backtick (`) is not a single quote (') Quote Link to comment https://forums.phpfreaks.com/topic/251329-cant-get-two-open-sql-connections-to-work/#findComment-1289152 Share on other sites More sharing options...
Mavrik347 Posted November 18, 2011 Author Share Posted November 18, 2011 Hehe I know that now Quote Link to comment https://forums.phpfreaks.com/topic/251329-cant-get-two-open-sql-connections-to-work/#findComment-1289153 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.