zoukankan      html  css  js  c++  java
  • PHP同时连接多个数据库

    PHP同时连接多个mysql数据库的具体实现

    方法一:
     1 <?php
     2 $conn1 = mysql_connect("127.0.0.1", "root","root","db1");
     3 mysql_select_db("Muma", $conn1);
     4 $conn2 = mysql_connect("127.0.0.1", "root","root","db2");
     5 mysql_select_db("product", $conn2);
     6  
     7 $sql = "select * from ip";
     8 $query = mysql_query($sql,$conn1); //添加连接$conn1
     9 if($row = mysql_fetch_array($query))
    10 echo $row[0]."
    ";
    11  
    12 $sql = "select * from web ";
    13 $query = mysql_query($sql, $conn2);
    14 if($row = mysql_fetch_array($query))
    15 echo $row[0];
    16 ?>
    方法2:在sql语句中关联所用数据库,此时可以省略mysql_query的第二个参数,即:
     1 <?php
     2 $conn1 = mysql_connect("127.0.0.1", "root","root","db1");
     3 mysql_select_db("db1", $conn1);
     4 $conn2 = mysql_connect("127.0.0.1", "root","root","db2");
     5 mysql_select_db("db2", $conn2);
     6  
     7 $sql = "select * from db1.ip"; //关联数据库
     8 $query = mysql_query($sql);
     9 if($row = mysql_fetch_array($query))
    10 echo $row[0]."
    ";
    11  
    12 $sql = "select * from db2.web ";
    13 $query = mysql_query($sql);
    14 if($row = mysql_fetch_array($query))
    15 echo $row[0];
    16 ?>
  • 相关阅读:
    javaee 第六周作业
    javaee 第五周作业
    javaee 第四周作业
    第三周作业
    第二周作业xml学习情况
    javaWeb 中http请求 get 与 post的区别
    第八周
    第七周
    第六周
    第五周
  • 原文地址:https://www.cnblogs.com/redfire/p/7695108.html
Copyright © 2011-2022 走看看