Popular Posts

Tuesday, 12 August 2014

How to connect two mysql databases in PHP?

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.


<?PHP
 
  
 $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>";
 }
 
/* NOTE :- 
 * 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.
 */

 ?>

No comments:

Post a Comment

Magento: How to get last order id

There are many ways to get last order id:   1. From checkout session: $lastOrderId = Mage::getSingleton('checkout/session'...