Jump to content

Random database?


Incredinot

Recommended Posts

Hi..

 

Alright, im trying to achieve something, but think i need a little help..

 

Lets say i have 10 MySQL database that are called test1, test2 etc.

(I will store all the database names somewhere in a table..) (The databases is in the same PHPmyadmin area, so the login will be the same, but database name will be diferent)

 

Now i randomly want to take 5 of those databases and show a random value from a table called testtable and field called testfield.

 

The thing is that i want to have 1 mainsite that take 5 random topic from my other pages..

 

Is it even possible... or well.. ofcourse it is... but should i consider someway else to do it, or aint it that tricky after all?

 

Hope all of you had a nice christmas and a great new year (:

 

Regards,

Mike

Link to comment
https://forums.phpfreaks.com/topic/187572-random-database/
Share on other sites

Yea, the switching between databases (open one, get something, close it, open another, do the same etc) was what i thought it would resolve in when i tried to brainstorm it myself...

The thing is that i aldready have 10 individual sites that are working just fine, so i cant just use tables, i will need to do the switch between the databases instead..

Link to comment
https://forums.phpfreaks.com/topic/187572-random-database/#findComment-990320
Share on other sites

Well I guess given the situation that you wish to pull the same fields from x amount of random databases that already exist the only real option is to simply loop through them creating a new database connection for each one, but I imagine it won't exactly be speedy.

Link to comment
https://forums.phpfreaks.com/topic/187572-random-database/#findComment-990324
Share on other sites

The databases is in the same PHPmyadmin area, so the login will be the same

 

Only one connection is needed if the username/password is the same.

 

You can either select the database using mysql_select_db() before a specific query or you can specify the database in the query using the db_name.table_name syntax instead of just using the table_name in the query.

Link to comment
https://forums.phpfreaks.com/topic/187572-random-database/#findComment-990329
Share on other sites

Untested, but should work -

 

<?php
$db_names = array('test1','test2','test3','test4','test5','test6','test7','test8','test9','test10'); // get the actual names of your databases into an array
shuffle($db_names); // randomize the names

$query = "SELECT testfield FROM $db_names[0].testtable
UNION SELECT testfield FROM $db_names[1].testtable
UNION SELECT testfield FROM $db_names[2].testtable 
UNION SELECT testfield FROM $db_names[3].testtable
UNION SELECT testfield FROM $db_names[4].testtable
";
?>

Link to comment
https://forums.phpfreaks.com/topic/187572-random-database/#findComment-990344
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.