Incredinot Posted January 7, 2010 Share Posted January 7, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/187572-random-database/ Share on other sites More sharing options...
cags Posted January 7, 2010 Share Posted January 7, 2010 I'm not sure I understand why you want independent databases, not simply tables. If you wish to work with multiple databases at the same time you will either have to create multiple database connection else keep switching between databases. Quote Link to comment https://forums.phpfreaks.com/topic/187572-random-database/#findComment-990314 Share on other sites More sharing options...
Incredinot Posted January 7, 2010 Author Share Posted January 7, 2010 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.. Quote Link to comment https://forums.phpfreaks.com/topic/187572-random-database/#findComment-990320 Share on other sites More sharing options...
cags Posted January 7, 2010 Share Posted January 7, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/187572-random-database/#findComment-990324 Share on other sites More sharing options...
PFMaBiSmAd Posted January 7, 2010 Share Posted January 7, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/187572-random-database/#findComment-990329 Share on other sites More sharing options...
cags Posted January 7, 2010 Share Posted January 7, 2010 My bad, I didn't notice that bit, I figured since there were 10 DBs for separate sites they were probably hosted seperately. Quote Link to comment https://forums.phpfreaks.com/topic/187572-random-database/#findComment-990331 Share on other sites More sharing options...
PFMaBiSmAd Posted January 7, 2010 Share Posted January 7, 2010 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 "; ?> Quote Link to comment https://forums.phpfreaks.com/topic/187572-random-database/#findComment-990344 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.