How to connect two mysql databases in PHP?

Downloads:2898 Bookmark this code Bookmark and Share
Rank : 4/5
Rated by : 4 user(s)
Developer:Andrew R  |   13 submission(s)
Date Uploaded:November 01,2009
Level:Intermediate
Size: 0 Bytes
Category:PHP -> Database
Developer Says:

The sample code below makes 2 database connections and the reference to each database connection is stored in separate variables. Whenever you connect multiple databases you have to specify the link in mysql_query function to tell PHP to run the query on the specified database.

<?
 
	/*
	*	The sample code below makes 2 database connections and the reference to each database connection is stored in separate variables.
	*   Whenever you connect multiple databases you have to specify the link in mysql_query function to tell PHP to run the query
	*   on the specified database.
	*/
 
	$link1=mysql_connect("localhost","root","");
	mysql_select_db("qlets");
 
 
	$link2=mysql_connect("localhost","root","",true);
	mysql_select_db("sargodhabiz",$link2);
 
	$result1=mysql_query("select * from portfolio",$link1);
	show_data($result1);
 
	$result2=mysql_query("select * from categories",$link2);
	show_data($result2);
 
	mysql_close($link1);
	mysql_close($link2);
 
 
	function show_data($result){
		$x=mysql_num_fields($result);
		echo "<table border=\"1\">"; 
		while($row=mysql_fetch_array($result)){
			echo "<tr>";			
			for($i=0;$i<$x;$i++){
				echo "<td>".$row[$i]."</td>";
			}
			echo "</tr>";
		}
		echo "</table>";
	}
 ?>


User Reviews
Chris
[30-Dec-2009]
#1

This code is going to be very useful to me.

Saima
[08-Feb-2011]
#2

THANKS FOR YOUR KIND HELP :)

Leave a Comment
Age (Required, will not be shown)
Name
Email (Required, will not be shown)
Website (Optional, starting with http://)
 
Rate this code
Poor   1 2 3 4 5   Outstanding
Are you human ?

Enter the code shown above