TapeGun007 Posted February 20, 2017 Share Posted February 20, 2017 I'm looking for a temporary solution to a database issue. Eventually, I will put an end to all of this nonsense, so for all those would reply "Why in the world would you do that?"... I didn't. Someone else did. I'm just looking for a temporary fix. I have a set of databases all on the same server, same fields, same log in credentials. I simply want to create a $con array because based on user selection, there may only be one database, or there could be as many as 10 (all of which I would use UNION to join them). I know this is a syntax error on my part, I cannot seem to Google the right information to understand how to do this properly. $con = array(); $arrlength = count($statedb); echo "$arrlength<br />"; for ($x = 0; $x < $arrlength; $x++) { $con($x) = new mysqli($DBServer, $DBUser, $DBPass, $statedb($x)); echo $con($x); } So in the example above, let's say California was selected, there are 10 databases (all identical in login, layout, but different information). I'd like there to basically be $con x 10. I think the code above is pretty self explanatory. Quote Link to comment https://forums.phpfreaks.com/topic/303251-can-you-store-con-into-an-array/ Share on other sites More sharing options...
Solution TapeGun007 Posted February 20, 2017 Author Solution Share Posted February 20, 2017 I think I have it already... I was too quick to post. I see my syntax error above as well. I think this will work: foreach($statedb as $x => $x_value){ $con[$x] = new mysqli($DBServer, $DBUser, $DBPass, $x_value); } Quote Link to comment https://forums.phpfreaks.com/topic/303251-can-you-store-con-into-an-array/#findComment-1543116 Share on other sites More sharing options...
ginerjm Posted February 20, 2017 Share Posted February 20, 2017 My question is not the one you dread, but "why do you believe you need different connections?". I believe that you can simply do your query (union) with just one db connection IF as you said they are all on the same server and all have the same login creds. Plus - how would you ever implement multiple connections in a query? The calls to query functions rely on a single connection, not an array of them. Case solved? As for the query you simply preface tablename with the dbname as in "dbname.tablename" Quote Link to comment https://forums.phpfreaks.com/topic/303251-can-you-store-con-into-an-array/#findComment-1543117 Share on other sites More sharing options...
Psycho Posted February 20, 2017 Share Posted February 20, 2017 (edited) My question is not the one you dread, but "why do you believe you need different connections?". I believe that you can simply do your query (union) with just one db connection IF as you said they are all on the same server and all have the same login creds. Plus - how would you ever implement multiple connections in a query? The calls to query functions rely on a single connection, not an array of them. Case solved? As for the query you simply preface tablename with the dbname as in "dbname.tablename" Pretty much what I was going to say. You just need an array of the database names along with a single connection. Then you can dynamically build the UNION queries something like this: //Create a query "mask" with placeholder for DB name $queryMask = "SELECT field1, field2, feild3 FROM [DB_NAME].tablename"; //Iterate over each DB creating the clause //for each DB replacing the mask $unionClauses = array(); foreach($databases as $db_name) { $unionClauses[] = str_replace('[DB_NAME]', $db_name); } //Cobine all the clauses with a UNION $query = implode("\nUNION\n", $unionClauses); //Using line break '\n' to make the query readable if you need to echo it out Edited February 20, 2017 by Psycho Quote Link to comment https://forums.phpfreaks.com/topic/303251-can-you-store-con-into-an-array/#findComment-1543120 Share on other sites More sharing options...
TapeGun007 Posted February 20, 2017 Author Share Posted February 20, 2017 (edited) Yes, I agree with both of you. It's because some states have a different # of databases. So if $statedb = "1", then "SELECT * FROM <db> WHERE... " else "SELECT * FROM <db> WHERE ... UNION..." Thanks for the code Psycho, I'm not familiar with implode so I'll go look that up. edit: You wrote what I wanted to do in a MUCH better way. Thanks again. Edited February 20, 2017 by TapeGun007 Quote Link to comment https://forums.phpfreaks.com/topic/303251-can-you-store-con-into-an-array/#findComment-1543123 Share on other sites More sharing options...
mac_gyver Posted February 20, 2017 Share Posted February 20, 2017 as to your connection code. the 4th parameter CAN be a database name, but it's optional. you can leave it out, provide the name of the common database that most of your application code uses, or select the database by separately calling the ->select_db(...) method. since your query will be specifying the database name in front of each table name, it doesn't matter if or what database is selected for the single connection. Quote Link to comment https://forums.phpfreaks.com/topic/303251-can-you-store-con-into-an-array/#findComment-1543127 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.