Home:ALL Converter>Looping through mysql database

Looping through mysql database

Ask Time:2011-05-25T22:16:09         Author:astymore

Json Formatter

I have series of mysql databases on my server. The names of these databases are stored in a separate database table (users) - column(dbname) . I want to loop through table (users), get the dbnames, connect to each and perform an operation on each database. How do I perform such looping?

Author:astymore,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/6125859/looping-through-mysql-database
Luke Stevenson :

Some rough code. Not debugged or tested...\n\n$masterDBHost = 'localhost';\n$masterDBUser = 'username';\n$masterDBPass = 'somethingSecret';\n$masterDBName = 'theDBname';\n\n$sqlToPerformOnEachDatabases = 'SELECT 1';\n\n// Connect to the Master Database\nif( !( $master = mysql_connect( $masterDBHost , $masterDBUser , $masterDBPass ) ) )\n die( 'MySQL Error - Cannot Connect to Master Server' );\nif( !mysql_select_db( $masterDBName , $master ) )\n die( 'MySQL Error - Cannot Connect to Master Database' );\n\n// Get the Other Databases to Connect to\n$databases = mysql_query( 'SELECT * FROM `databaseTable`' , $master );\n\n// Check your Results\nif( !$databases || mysql_num_rows( $databases )==0 ){\n # Nothing to work with\n echo 'Unable to find Databases to Access';\n}else{\n # Something to work with\n while( $d = mysql_fetch_assoc( $databases ) ){\n // Connect to the Client Server\n if( !( $temp = mysql_connect( $d['host'] , $d['user'] , $d['pass'] ) ) ){\n # Can't connect to the Server\n echo 'MySQL Error - Failed to connect to '.$d['host'].' as '.$d['user'];\n }elseif( !mysql_select_db( $d['base'] , $temp ) ){\n # Can't connect to the Database\n echo 'MySQL Error - Failed to connect to '.$d['base'].' on '.$d['host'].' as '.$d['user'];\n }elseif( !mysql_query( $sqlToPerformOnEachDatabases , $temp ) ){\n # Your Command, well, stuffed up\n echo 'MySQL Error - Your Command Stuffed Up';\n }else{\n # Your Command worked OK\n echo 'All Good!';\n }\n # Close the connection (probably not needed, but nice to do)\n @mysql_close( $temp );\n }\n}\n\n\nVersion 2\n\nAllows for keeping a connection if the same Host/User/Pass is used\n\nAgain, not debugged or tested.\n\n$masterDBHost = 'localhost';\n$masterDBUser = 'username';\n$masterDBPass = 'somethingSecret';\n$masterDBName = 'theDBname';\n\n$sqlToPerformOnEachDatabases = 'SELECT 1';\n\n// Connect to the Master Database\nif( !( $master = mysql_connect( $masterDBHost , $masterDBUser , $masterDBPass ) ) )\n die( 'MySQL Error - Cannot Connect to Master Server' );\nif( !mysql_select_db( $masterDBName , $master ) )\n die( 'MySQL Error - Cannot Connect to Master Database' );\n\n// Get the Other Databases to Connect to\n$databases = mysql_query( 'SELECT * FROM `databaseTable`' , $master );\n\n// Check your Results\nif( !$databases || mysql_num_rows( $databases )==0 ){\n # Nothing to work with\n echo 'Unable to find Databases to Access';\n}else{\n # Something to work with\n // A variable for the MySQL Connection\n $temp = false;\n // Declare some short-term memory\n $last = array();\n while( $d = mysql_fetch_assoc( $databases ) ){\n // Check Last Loop's details\n if( $temp\n && $last\n && array_diff( $last , $d ) ){\n // New Host, User or Pass\n @mysql_close( $temp );\n $last = false;\n }\n // Connect to the Client Server\n if( !$last\n && !( $temp = mysql_connect( $d['host'] , $d['user'] , $d['pass'] ) ) ){\n # Can't connect to the Server\n echo 'MySQL Error - Failed to connect to '.$d['host'].' as '.$d['user'];\n }elseif( !mysql_select_db( $d['base'] , $temp ) ){\n # Can't connect to the Database\n echo 'MySQL Error - Failed to connect to '.$d['base'].' on '.$d['host'].' as '.$d['user'];\n }elseif( !mysql_query( $sqlToPerformOnEachDatabases , $temp ) ){\n # Your Command, well, stuffed up\n echo 'MySQL Error - Your Command Stuffed Up';\n }else{\n # Your Command worked OK\n echo 'All Good!';\n }\n # Remember this Loop's details\n $last = $d;\n }\n @mysql_close( $temp );\n}\n",
2011-05-25T14:35:27
astymore :

After days of struggle I was able to come out with this solution. I took a lot of ideas from solutions that were provided to this question on this platform. the solutions themselves did not work for me but I had lots of ideas from them. \n\n<?php\n\n //db parameters\n $dbhost = \"myhost\"; // this will ususally be 'localhost', but can sometimes differ \n $dbname = \"dbusers\"; // the name of the database that you are going to use for this project \n $dbuser = \"root\"; // the username that you created, or were given, to access your database \n $dbpass = \"mypassword\"; // the password that you created, or were given, to access your database \n\n\n // I first connect to the db with names of the other dbs \n mysql_connect($dbhost, $dbuser, $dbpass) or die(\"MySQL Error: \" . mysql_error()); \n mysql_select_db($dbname) or die(\"MySQL Error: \" . mysql_error()); \n\n\n //select dbnames from the table with db names\n $query = \"SELECT dbname FROM users\";\n $result = mysql_query($query);\n\n\n //loop through the results(dbname) and connect to each db \n while($row = mysql_fetch_array($result, MYSQL_ASSOC))\n {\n //put the dbname results into a variable\n $dbName =$row['dbname'];\n // connect to each db\n $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die(\"MySQL Error: \" . mysql_error()); \n $db = mysql_select_db(\"mydbprefix_\".$dbName) ;\n\n\n\n //do a query for each db this is because each db has same tables and structure\n //check to see if db exist\n if( $db ){\n\n //all the operation goes here in my case I wanted to count number of products for each table called products.\n $query2 = mysql_query(\"SELECT * FROM products\");\n\n $num_rows = mysql_num_rows($query2);\n $pro = $num_rows;\n echo \"this user has \".$pro.\" products\".\"<br/>\";\n\n //another operation goes here for the same db.\n\n // then close connection for each db\n mysql_Close ($conn);\n }\n } \n ?>\n\n\nThis was how I went around this problem. It might not be the best solution but this is what I have",
2011-06-02T11:18:59
yy