Login

Username or Password is Incorrect!
Enter your Username and Password!

Username

Password

Forgot passsword?

Topics: 15   |   Posts: 10   |   Comments: 12   |   Members: 151


MySQL Operations

Instantly Switching MySQL Database with PHP [ last update: Dec-31-69 06:00 pm ]

If you are working with PHP and have configuration file similar to below while trying to switch between MySQL databases for minimal information gathering then here is a basic solution. Database connection file:


<?php
$dbhost=\"localhost\";
$dbname=\"mydb\";
$dbuser=\"username\";
$dbpasswd=\"password11\";
// connect to the db
$link = mysql_connect($dbhost, $dbuser, $dbpasswd);
if (!$link) {
    die(\'Could not connect: \' . mysql_error());
}
$db_selected = mysql_select_db($dbname, $link);
if (!$db_selected) {
    die (\'Can\\\'t use dbreviews : \' . mysql_error());
}
?>

Now let\'s say you are outputting information using that configuration and want to get that bit from another database called \"mydb2\".

$data = mysql_query(\"SELECT * FROM table WHERE ID = \'5\'\") or die(mysql_error());
$info = mysql_fetch_array($data);

$title = $info[\'title\'];

// Switch to other database
$dbname2 = \"mydb2\";
mysql_select_db($dbname2, $link);

// Get information from table in second database
$data2 = mysql_query(\"SELECT * FROM table WHERE ID = \'5\'\") or die(mysql_error());
$info2 = mysql_fetch_array($data2);

$description = $info2[\'description\'];

// Switch back to original database
$dbname = \"mydb\";
mysql_select_db($dbname, $link);

This would allow you to instantly move from the original database, to another database and back without any affiliated issues.

0

replies

Topic Search
Get Started