Solved

phpmyadmin multiple servers

Posted on 2011-10-01
2
286 Views
Last Modified: 2012-05-12
I just installed php_myadmin.  I had a very old copy and the new version is much better.  I want to be able to connect to multiple databases, but I am not sure how.  I have a reseller account, and all account are on the same server.  I connect to each database using localhost, in their own domains (same ip though).  

Also, it doesn't ask for a user name or password, don't I have to supply those?

/*
 * Second server
 */
$i++;
/* Authentication type */
$cfg['Servers'][$i]['auth_type'] = 'cookie';
/* Server parameters */
$cfg['Servers'][$i]['host'] = 'localhost';
$cfg['Servers'][$i]['connect_type'] = 'tcp';
$cfg['Servers'][$i]['compress'] = false;
/* Select mysqli if your server has it */
$cfg['Servers'][$i]['extension'] = 'mysql';
$cfg['Servers'][$i]['AllowNoPassword'] = false;
0
Comment
Question by:jackjohnson44
2 Comments
 
LVL 20

Accepted Solution

by:
Mark Brady earned 500 total points
Comment Utility
If the user account you are using has the correct privillages and can connect to more than just one database (you need to add the username to each database you want to connect to) then you can simply connect to the first database like this:

mysql_connect($SERVER_NAME, DB_USER, DB_PASSWORD);
// then this line to use that database each time you want to change databases....

mysql_select_db(DB_NAME);

Now you are connected and can use the first database. When you want to swap and use another database (provided the username is the same for both databases) use this line....

mysql_select_db(DB_NAME1); // where DB_NAME AND DB_NAME! are the database names you want to work with.

The best way to do this and it is very tidy is to make a file called "constants.php" and inside the file you can setup your entire variable set like database connection details for each database. Here is a sample of what I use. You then include this file into every php script that you need to connect to your databases.. Here is the file..

/* constants.php */
<?php
/* Turn all error warnings and displays off for security and on for debugging */
ini_set("display_errors","off");
error_reporting(0);  // Uncomment this line and comment out next line before making this script public
//error_reporting(E_ALL);
date_default_timezone_set('Antarctica/McMurdo'); // use your timezone here

define("DB_SERVER", "localhost"); // change this to suit your servername - usually localhost
define("DB_USER", "type_your_username_here"); // your DB username
define("DB_PASS", "your_password_here");
define("DB_NAME", "your_database_name_here");
define("DB_NAME1", "your_database_name1_here");
define("DB_NAME3", "your_database_name2_here");

// Now here is the connection script to connect to DB_NAME
mysql_connect(DB_SERVER, DB_USER, DB_PASS); // connect to localhost or your server
mysql_select_db(DB_NAME); // select the first database to work with
?>

Now by including this saved file with any script, that script is ready to start doing mysql queries. You have already selected the first database (DB_NAME) so do whatever you need to then to select another database somewhere in your php scripts use this line

mysql_select_db(DB_NAME1); // now you are working with the database stored in the variable "DB_NAME1".

Do what you need with database1 then either select the next database the same way or slect the first database again but each database has beed stored in a global variable named "DB_NAME" or "DB_NAME1" or "DB_NAME2".

Just switch between the databases with the line "mysql_select_db("then your database name here");
and you can start performing queries on the new database.

Of course if the username you use to connect to the server only has privillages on one database then you would need to either add that username to the other databases and set privillages for the username, or, close the connection you have each time you want to change, then open a new connection to the server with a different username/password combination. Then select the database that the username is allowed to use.

I hope that is easy enough for you to follow.
0
 

Author Comment

by:jackjohnson44
Comment Utility
Thanks, but this is a question about phpmyadmin.  I know how to create a new database connection in code that I write.  phpmyadmin is a mysql management tool with a lot going on.  You can edit the config file and should be able to connect to multiple databases easily.  My question revolves around editing this file.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Creating and Managing Databases with phpMyAdmin in cPanel.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now