How can one effectively manage multiple databases on a server when connecting to them in PHP?
To effectively manage multiple databases on a server when connecting to them in PHP, you can create separate database connection objects for each database. This allows you to interact with each database independently and execute queries as needed.
// Database connection for database 1
$servername1 = "localhost";
$username1 = "username";
$password1 = "password";
$dbname1 = "database1";
$conn1 = new mysqli($servername1, $username1, $password1, $dbname1);
// Database connection for database 2
$servername2 = "localhost";
$username2 = "username";
$password2 = "password";
$dbname2 = "database2";
$conn2 = new mysqli($servername2, $username2, $password2, $dbname2);
// Perform queries on database 1
$sql1 = "SELECT * FROM table1";
$result1 = $conn1->query($sql1);
// Perform queries on database 2
$sql2 = "SELECT * FROM table2";
$result2 = $conn2->query($sql2);
// Close connections
$conn1->close();
$conn2->close();
Keywords
Related Questions
- What is the best practice for formatting PHP output with CSS to achieve different font sizes and backgrounds?
- What are the potential implications of not using mysql_real_escape_string when magic_quotes are enabled?
- Are there any best practices for handling CRUD database operations in PHP for tasks such as creating projects and assigning users as project leaders?