Connecting to Two Databases PHP
This Tutorial is to view data from two data base table. you have created table division in my_database in previous lesson create table division
CREATE TABLE IF NOT EXISTS `division` ( `uid` int(40) NOT NULL AUTO_INCREMENT, `uname` varchar(45) NOT NULL, `division` varchar(45) NOT NULL, PRIMARY KEY (`uid`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1; |
Then create table employee in oops database
CREATE TABLE IF NOT EXISTS `employee` ( `ID` int(20) NOT NULL AUTO_INCREMENT, `Name` varchar(50) NOT NULL, `Surname` varchar(30) NOT NULL, PRIMARY KEY (`ID`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1; |
Next creating bellow php file you can see the data in two table from two databases
ID | Name | Division |
---|---|---|
1 | Diana | Electronic |
2 | Jack | Computer |
ID | Name | Surname |
---|---|---|
1 | Larisa | Volk |
2 | Oksana | Babiak |
<?php $hostname="localhost"; $username="root"; $password=""; $con1 = mysql_connect("localhost","root",""); if (!$con1) { die('Could not connect: ' . mysql_error()); } $con2 = mysql_connect("localhost","root",""); if (!$con2) { die('Could not connect: ' . mysql_error()); } mysql_select_db('my_database',$con1); $query1=mysql_query('select * from divition'); echo "<table border='1'> <tr> <th>ID</th> <th>Name</th> <th>Division</th> </tr>"; while($row = mysql_fetch_array($query1)) { echo "<tr>"; echo "<td>" . $row['uid'] . "</td>"; echo "<td>" . $row['uname'] . "</td>"; echo "<td>" . $row['division'] . "</td>"; echo "</tr>"; } echo "</table>"; echo "</br>"; mysql_select_db('oops', $con2); $query2=mysql_query('select * from employee'); echo "<table border='1'> <tr> <th>ID</th> <th>Name</th> <th>Surname</th> </tr>"; while($row = mysql_fetch_array($query2)) { echo "<tr>"; echo "<td>" . $row['ID'] . "</td>"; echo "<td>" . $row['Name'] . "</td>"; echo "<td>" . $row['Surname'] . "</td>"; echo "</tr>"; } echo "</table>"; ?> |
http://www.larryullman.com/forums/index.php?/topic/1515-can-you-run-two-databases-simultaneously/
ReplyDelete