Video tutorial: connect to multiple databases on a single PHP page

In this tutorial I want to explain how is possible to connect to multiple databases on a single PHP. I have two database, but you can use mare database.

First, I will create my first connection:

$db1 = mysql_connect("localhost","user","");
mysql_select_db("database1",$db1);

Then, I will extract the data:

$ex1 = mysql_query("SELECT * FROM `table`",$db1); // you must only add $db1 to use the first database
echo mysql_result($ex1,0,"title")."
";

Next, I will create my second connection:

$db2 = mysql_connect("localhost","user","");
mysql_select_db("database2",$db2);

And now I will extract the data from my second database:

$ex2 = mysql_query("SELECT * FROM `table`",$db2);
echo mysql_result($ex2,0,"title")."
";

In this way you can connect to multiple databases on a single PHP page.

You can see the results of this code in the video bellow:









Leave a Comment