How to connect two mysql databases in PHP?

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 "\"1\">";    while($row=mysql_fetch_array($result)){echo"";       for($i=0;$i<$x;$i++){echo"";    }echo"
";   }echo"
".$row[$i]."
"
;



(www.qualitycode.com)

0 comments:

Post a Comment