解决方案 »

  1.   


    $con = mysql_connect('127.0.0.1','root','');
    mysql_select_db('data1',$con);
    $result = mysql_query('select xm from user"',$con);$conn = mysql_connect('127.0.0.2','root','');
    mysql_select_db('data2',$conn);while ($row = mysql_fetch_array($result)){
         $sql = 'insert into data2.chart(xm)value("'.$row['xm'].'")';
         $ret = mysql_query($sql,$conn);
    }
      

  2.   

    $sql = 'insert into data2.chart(xm)values("'.$row['xm'].'")';
      

  3.   

    楼主的问题
    php在一个页面如何实现同时读取两个数据库
      

  4.   

    如果是2个数据库在一个服务器, 如果你一个mysql帐号有2个表的权限。
    那就很简单。#2的方法就可以
    如果是2个服务器, 那么用#1的的方法。
    #1的方法我试过。 可以的。
      

  5.   

    谢谢你们的帮助,能在帮仔细看看如何实现吗?对PHP我实在不是很懂!再次谢谢大家给予的帮助了!
    具体是我想从登录页获取ID在根据ID获取姓名,再在点击发言时将姓名存入数据库显示在页面上。时间和发言内容的添加我已经完成了。就差姓名了。
    首先这是我做的登录页
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>用户登录</title>
    <style type="text/css">
    #apDiv1 {
    position: absolute;
    width: 686px;
    height: 392px;
    z-index: 1;
    left: 458px;
    top: 152px;
    overflow: visible;
    }
    #apDiv2 {
    position:absolute;
    width:312px;
    height:317px;
    z-index:1;
    top: 26px;
    left: 25px;
    }
    #apDiv3 {
    position: absolute;
    width: 330px;
    height: 333px;
    z-index: 1;
    left: 351px;
    top: 19px;
    font-size: 24px;
    }
    </style>
    <link href="other/wenzi.css" rel="stylesheet" type="text/css" />
    <style type="text/css">
    body {
    background-image: url(images/loginbj.jpg);
    background-repeat: repeat-x;
    }
    tishi {
    font-size: 14px;
    }
    </style>
    <!--连接数据库操作-->
    <?php
    if($_GET['a']=="")
    {
    $hy="欢迎您,请登录";
    }
    else
    {
    $link=mysql_connect("localhost","root","123");
    if(!$link) echo "连接数据库失败";
    $user=$_POST["username"];
    $pass=$_POST["password"];
    mysql_select_db("liaotianshi",$link);
    $aa=mysql_query("select * from t_user where username='".$user."' and password='".$pass."'");
    $all=mysql_fetch_array($aa);
    if($all)
    {
    header("Location:index.php");
    }
    else
    {
        $ts="登录失败!请检查用户名和密码!";
    }
    mysql_close($link);
    }
    ?>
    <!--/连接数据库操作/-->
    <!--用户名和密码的判断-->
    <script type="text/javascript"> 
    function login(){ 
    if(form1.username.value==""){ 
    alert("请输入用户名!"); 
    return false; 

    if(form1.password.value==""){ 
    alert("请输入密码!"); 
    return false; 

    }
    </script>
    <!--/用户名和密码的判断/-->
    </head>
    <body>
    <div id="apDiv1" border="3">
      <table width="685" height="372" border="5" bgcolor="#FAF8F3">
        <tr>
          <td><div id="apDiv2">
        <table width="332" border="0">
          <tr>
            <td height="304"><img src="images/tb.jpg" width="312px" height="317px"/></td>
          </tr>
        </table>
      </div>
      <div id="apDiv3">
      <form id="form1" name="form1" method="post" action="?a=ok">
        <table width="327" height="310" border="0">
          <tr>
            <td width="74" height="96" rowspan="2">&nbsp;</td>
            <td width="144" height="65" align="center">聊天室登录</td>
            <td width="79" rowspan="2">&nbsp;</td>
          </tr>
          <tr>
            <td height="29" align="center" class="wenzi"><font color="#FF0000"><?php echo "$ts"; ?></font><font color="bule"><?php echo "$hy"?></font></td>
          </tr>
          <tr>
            <td height="50" align="right" valign="middle"class="wenzi">用户名:</td>
            <td><input type="text" name="username"/></td>
            <td>&nbsp;</td>
          </tr>
          <tr>
            <td height="50" align="right" valign="middle" class="wenzi">密码:</td>
            <td><input type="password" name="password"/></td>
            <td>&nbsp;</td>
          </tr>
          <tr>
            <td height="35" colspan="3" align="center" valign="top"><input type="submit" value="登陆" onclick="login()"/>&nbsp;&nbsp;<input type="reset" value="重置" /></td>
          </tr>
          <tr>
            <td height="21" colspan="3" align="center" valign="top" class="wenzi">如果没有账号,请<a href="add.php">[注册]</a>!</td>
          </tr>
          <tr>
            <td height="51" colspan="3" align="center" valign="top" class="wenzi"><a href="login.php">【管理员登陆】</a></td>
          </tr>
        </table>
        </form>
      </div></td>
        </tr>
      </table>
    </div>
    </body>
    </html>
    下面这是我的聊天室页面
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>聊天室</title><style type="text/css">
    #apDiv1 {
    position: absolute;
    width: 508px;
    height: 494px;
    z-index: 1;
    left: 523px;
    top: 140px;
    }
    body {
    background-color: #27b9d0;
    }
    </style>
    <link href="css/index-wenzi.css" rel="stylesheet" type="text/css" />
    <!--查询操作-->
    <?php
    $link=mysql_connect("localhost","root","123");
    if(!$link) echo "连接数据库失败";
    mysql_select_db("liaotianshi",$link);
    $aa=mysql_query("select * from t_chat");
    ?>
    <!--添加操作-->
    <?php
    if($_GET['a']=="")
    {
    }
    else
    {
    $link=mysql_connect("localhost","root","123");
    if(!$link) echo "连接数据库失败";
    $user=$_POST["username"];
    $con=$_POST["content"];
    $currtime=date("Y-m-d h:i:s");
    $xm=$_POST["xm"];
    mysql_select_db("liaotianshi",$link); $tj=mysql_query("insert into t_chat(username,content,addtime,xm) values('".$user."','".$con."','".$currtime."','".$xm."')");
    mysql_close($link);
    header("location:index.php");
    }
    ?>
    <!--删除判断-->
    <script language="javascript">
    function delete_confirm(){
    var r=confirm("确认删除?")
             if (r==true){
                 return true;
             }else{
                 return false;
             } 
          }  
    </script>
    </head><body>
    <div id="apDiv1">
      <table width="505" border="1">
        <tr>
          <td colspan="2" align="center" class="test">13计2聊天室 </td>
          <td colspan="2" align="center" class="test">当前用户:</td>
        </tr>
    <tr class="test">
          <td align="center">发言人</td>
          <td align="center">发言内容</td>
          <td align="center">发言时间</td>
          <td align="center">操作</td>
          
        </tr>
        <!--循环输出聊天记录-->
      <?php
      while($all=mysql_fetch_array($aa)){
      ?>
        <tr>
          <td width="86" align="center" class="test"><?php echo $all['xm'];?></td>
          <td width="259" align="center" class="test"><?php echo $all['content'];?></td>
          <td width="101" align="center" class="test"><?php echo $all['addtime'];?></td>
          <td align="center"><a href="shanchu.php?id=<?php echo $all['id'];?>" onClick="return delete_confirm();">删除</a></td>
        </tr>
      <?php
      }
      mysql_close($link);
      ?> 
      </table>
      
    <form id="form1" name="form1" method="post" action="?a=ok">
      <table width="501" border="1">
        <tr>
          <td width="470" align="center" class="test">内容:
            <label for="content"></label>
          <input type="text" name="content" id="content" />
          <input name="提交" type="submit" value="发言" />
          </td>
        </tr>
      </table>
    </form>
    </div>
    </body>
    </html>