Jump to content

Can you store $con into an array?


Go to solution Solved by TapeGun007,

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/303251-can-you-store-con-into-an-array/
Share on other sites

  • Solution

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);
}

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"

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 by Psycho

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 by TapeGun007

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.