连接2个不同的数据库不起作用

时间:2013-02-08 07:11:45

标签: php mysql

enter image description here

首先,我已经知道如何不建议使用mysql_connect,但我没有时间学习PDO命令,一旦我解决了这个问题,我将把它改为PDO。但是现在,我仍然无法在每个文件上合并两个具有不同数据库连接的代码。

<?php



    $dbh1 = mysql_connect('localhost', 'root', ''); 
    $dbh2 = mysql_connect('localhost', 'root', '', true); 

    mysql_select_db('invoices', $dbh1);
    mysql_select_db('hmis', $dbh2);

    $id = '17';

    /////

      if ($id) {
    echo "<h1>Edit Medical Records # : $id</h1>";
    }
    else
    {
    echo " <h1>Medical Records</h1>";
    }


    if($submit)
    {
    //-------------------check user name-----------------------------------------------------
    $name = $_POST['name'];

    $select = "select first_name from patient where last_name = '".$last_name."';";
    $query = mysql_query($select,$dbh1) or die ("Mysql error! It was: ".mysql_error());
    $rows = mysql_num_rows($query);
    if ($rows != 0)
    {

      echo ("<script language=javascript>alert ('Sorry! $first_name is already taken!')</script>");
      echo ("<script language=javascript>document.location.href = 'editclient.php'</script>");

    exit;
    }
    if($name == "admin"){
      echo ("<script language=javascript>alert ('Sorry! $first_name is not a good thing to do!')</script>");
      echo ("<script language=javascript>document.location.href = 'editclient.php'</script>");
    exit;

    }
    //-----------------------------------end check-------------------------------------------
    $sql = "INSERT INTO patient (`first_name`, `last_name`,`country`, `address`, `gender`, `telno`, `occ`, `spo`, `occ2`,`cp`, `tel2`, `reff`, `all`, `ill`, `mat`, `frat`, `datetime`, `vs`,`ppe`, `ffup`)
    VALUES ('$first_name','$last_name','$country','$address','$gender','$telno','$occ','$spo','$occ2', '$cp','$tel2','$reff','$all','$ill','$mat','$frat','$datetime','$vs','$ppe','$ffup')";
    $result = mysql_query($sql,$dbh1);
    //echo ("<script language=javascript>document.location.href = 'editclient.php?id=$id&message=Patient $id has been Created!'</script>");
    echo ("<SCRIPT LANGUAGE='JavaScript'>
        window.alert('Succesfully Updated')
        window.location.href='sample4.php';
        </SCRIPT>");

    }
    else if($update)
    {

    $sql = "UPDATE patient set first_name='$first_name',last_name='$last_name',
    country='$country',address='$address',gender='$gender',telno='$telno',
    occ='$occ',spo='$spo',occ2='$occ2', cp='$cp', tel2='$tel2',reff='$reff', ill='$ill', mat='$mat', frat='$frat', datetime='$datetime', vs='$vs', ppe='$ppe' ,ffup='$ffup'
    WHERE clientid=$id";
    $result = mysql_query($sql ,$dbh1);
    //echo ("<script language=javascript>document.location.href = 'editclient.php?id=$id&message=Client $id has been Updated!'</script>");
    echo ("<SCRIPT LANGUAGE='JavaScript'>
        window.alert('Succesfully Updated')
        window.location.href='sample4.php';
        </SCRIPT>");


    }
    else if($id)
    {
    $result = mysql_query("SELECT * FROM patient WHERE clientid=$id",$dbh1);
    $row = mysql_fetch_array($result);

    ?>
    </p>
    <br><font color=red size=4><?php if (isset($_GET['message'])) { echo ' - '.$_GET['message']; } ?></font><BR>
    <?

    //-------------NEXT PREVIOUS----------------------------------------------------------------------

    $sql ="SELECT * FROM patient WHERE clientid < '$id' ORDER by clientid DESC LIMIT 1";

    $resultp = mysql_query($sql ,$dbh1);
    $previousrows = mysql_num_rows($resultp);
    while ($prerow = mysql_fetch_array($resultp)) {
    $prev=$prerow['clientid'];
    }
    //echo $prev;

    $sql2 ="SELECT clientid FROM patient WHERE clientid > '$id' ORDER by clientid ASC LIMIT 1";

    $resultn = mysql_query($sql2,$dbh1);
    $nextrows = mysql_num_rows($resultn);
    while ($nextrow = mysql_fetch_array($resultn)) {
    $next=$nextrow['clientid'];
    }
    //echo $next;
    if ($previousrows == "") {
    echo "";
    }else{
    echo "<B><a href=$PHP_SELF?id=$prev>$prev <- Prev</a> | ";
    }
    echo "<font size=6><B>$id</B></font>";
    if ($nextrows == "") {
    echo "";
    }else{
    echo "<b> | <a href=$PHP_SELF?id=$next > Next -> $next</a></b>";
    }
    //-----------------end next prev-----------------------------------------------------
    ?><br><br>
    <?php include("inc/nav.inc");?>
    <form name="form2a" method="post" action="<?php echo $PHP_SELF?>">
       <table width="760" border="0" cellspacing="0" cellpadding="0">
        <tr>
          <td class=topHeadrow1 valign="middle" width="50%" colspan="2">
    Edit Patient Information:</td>
          <td class=topHeadrow1 valign="middle" width="10" rowspan="20">
    &nbsp;</td>
          <td class=topHeadrow1 valign="middle" width="50%">
    Procedures:</td>
        </tr>
        <tr>
          <td valign="top" align="right">
    <input type="hidden" name="id" value="<?php echo $row["clientid"]?>">
    First name:<br>
    </td>
          <td valign="top" align="left">
    &nbsp;<input type="text" size="20" name="first_name" value="<?php echo $row["first_name"]?>" ></td>
          <td valign="top" width="10%" rowspan="13">




          <!-- edit include inv -->

          <?php //include ('testinclude.php'); ?>

            <!-- END -->




          </td>
       </tr>
        <tr>
          <td valign="top" align="right">
    Last Name:
    </td>
          <td valign="top" align="left">
    &nbsp;<input type="text" name="last_name" size="20" value="<?php echo $row["last_name"]?>"></td>
       </tr>


       <tr> 
        <td valign="top" align="right"> Gender:</td>
          <td valign="top" align="left"><select name="gender">
          <option value="Male">Male</option>
          <option value="Female">Female</option> </select> 
             </td>
        </tr>
        <tr> 
        <td valign="top" align="right">  Country:</td>
      <td valign="top" align="left">
          <select name="country" >
            <option value="Afghanistan">Afghanistan</option>
            <option value="Bahrain">Bahrain</option>

    <option value="Bangladesh">Bangladesh</option>
    <option value="Bhutan">Bhutan</option>
    <option value="Brunei">Brunei</option>
    <option value="Cambodia">Cambodia</option>
            <option value="China">China</option>
            <option value="Malaysia">Malaysia</option>


    <option value="Maldives">Maldives</option>
    <option value="Mongolia">Mongolia</option>
    <option value="Myanmar">Myanmar (Burma)</option>
    <option value="Pakistan">Pakistan</option>
    <option value="Philippines">Philippines</option>
    <option value="Saudi Arabia">Saudi Arabia</option>
    <option value="Singapore">Singapore</option>
    <option value="Sri Lanka">Sri Lanka</option>
    <option value="Syria">Syria</option>
    <option value="UAE">United Arab Emirates</option>
    <option value="Uzbekistan">Uzbekistan</option>

        </select> 

        </tr>

            <tr>
          <td valign="top" align="right">
    Tel. no.:</td>
          <td valign="top" align="left">
    <input
     type="text" size="20" name="telno" placeholder="###-####" value="<?php echo $row["telno"]?>"></td></tr>
     <tr>
          <td valign="top" align="right">
    Occupation:</td>
          <td valign="top" align="left">
    <input
     type="text" size="20" name="occ" value="<?php echo $row["occ"]?>"></td></tr>
     <tr>
          <td align="right">
    Spouse:</td>
          <td >
    <input
     type="text" size="20" name="spo" value="<?php echo $row["spo"]?>"></td></tr><tr>
      <td align="right">
    Occupation:</td>
          <td >
    <input
     type="text" size="20" name="occ2" value="<?php echo $row["occ2"]?>"></td></tr>



     <tr>
          <td align="right" width="20">
    Contact Person(in case of emergency):</td>
          <td >
    <input
     type="text" size="20" name="cp" value="<?php echo $row["cp"]?>"></td></tr><tr>
      <td align="right">
    Tel. no:</td>
          <td >
    <input
     type="text" size="20" name="tel2" value="<?php echo $row["tel2"]?>"></td></tr>

        <tr>
          <td valign="top" align="right" nowrap>
    Address:</td>
          <td valign="top" align="left">
    <input type="text" name="address" value="<?php echo $row["address"]?>" size="31"></td>
          </tr>

          <tr>
          <td valign="top" align="right" nowrap>
    Reffered By:</td>
          <td valign="top" align="left">
    <input type="text" name="reff" value="<?php echo $row["reff"]?>" size="31" ></td>
          </tr>

              <tr>
          <td class=topHeadrow1 valign="middle" width="50%" colspan="2">
    Past Medical History:</td>
          </tr>
        <tr>
          <td valign="top" align="right" nowrap>
    Allergy:</td>
          <td valign="top" align="left">
    <input type="text" name="all" value="<?php echo $row["all"]?>" size="31"></td>
          </tr> 
          <tr>
          <td valign="top" align="right" nowrap>
    Previous illness:</td>
          <td valign="top" align="left">
    <input type="text" name="ill" value="<?php echo $row["ill"]?>" size="31"></td>
          </tr>

        <tr>
         <td class=topHeadrow1 valign="middle" width="50%" colspan="2">
    <b><font size="4">
    Heredo-Familial Diseases:</font></b></td>
          </tr>
        <tr>
          <td valign="top" align="right" nowrap>
    Maternal:
         </td>
          <td valign="top" align="left">
    <input type="text" name="mat" value="<?php echo $row["mat"]?>" size="20"></td>
          </tr>
        <tr>
          <td valign="top" align="right" nowrap>
    Fraternal:
         </td>
          <td valign="top" align="left">
    <input type="text" name="frat" value="<?php echo $row["frat"]?>" size="20"></td>
          </tr>

    <tr>
         <td class=topHeadrow1 valign="middle" width="50%" colspan="2">
    <b><font size="4">
    Examination:</font></b></td>
          </tr>   
    <tr>
          <td valign="top" align="right" nowrap>
    Date / Time:</td>
          <td valign="top" align="left">
    <input type="text" name="datetime" value="<?php echo $row["datetime"]?>" size="31"></td>
          </tr>
          <tr>
          <td valign="top" align="right" nowrap>
    Vital Signs:</td>
          <td valign="top" align="left">
    <input type="text" name="vs" value="<?php echo $row["vs"]?>" size="31"></td>
          </tr>
          <tr>
          <td align="center" nowrap>
    Complain PPE Findings:</td>
          <td valign="top" align="left">
    <input type="text" name="ppe" value="<?php echo $row["ppe"]?>" size="31"></td>
          </tr>
           <tr>
          <td align="center" nowrap>
    Plan / Management / FF-UP</td>
          <td valign="top" align="left">
    <input type="text" name="ffup" value="<?php echo $row["ffup"]?>" size="31"></td>
          </tr>

        <tr>
          <td class=topHeadrow1 valign="middle" width="100%" colspan="4">
    <p align="center"><input class="form-button" type="Submit" name="update" value="Update Information"></td>
        </tr>
        <tr>
          <td valign="top" width="100%" colspan="4">
    <br>
    </td>
        </tr>
      </table>


      </form>

    <?
    }
    else
    {
    //----------------------------begin add client--------------------------------------------------------
    ?>
    <br><font color=red size=4><?php if (isset($_GET['message'])) { echo ' - '.$_GET['message']; } ?></font><BR>

    <form name="form2b" method="post" action="editclient.php" onSubmit="return checkPw(this)">
      <table>
        <tr>
          <td class="topHeadrow5" valign="top" width="100%" colspan="5">
    <b><font size="4">Patient Information:</font></b></td>
        </tr>
        <tr>
          <td valign="top" align="right">
    First Name:</td>
          <td valign="top" align="left">
    <input
     type="text" size="20" name="first_name" ></td></tr>
        <tr>
          <td valign="top" align="right">
    Last Name:</td>
          <td valign="top" align="left">
    <input type="text" name="last_name" size="20"></td></tr>
        <tr> 
        <td valign="top" align="right"> Gender:</td>
          <td valign="top" align="left"><select name="gender">
          <option value="Male">Male</option>
          <option value="Female">Female</option> </select> 
               </td>
        </tr>
        <tr> 
        <td valign="top" align="right">Country:</td>
      <td valign="top" align="left">
          <select name="country" >
            <option value="Afghanistan">Afghanistan</option>
            <option value="Bahrain">Bahrain</option>

    <option value="Bangladesh">Bangladesh</option>
    <option value="Bhutan">Bhutan</option>
    <option value="Brunei">Brunei</option>
    <option value="Cambodia">Cambodia</option>
            <option value="China">China</option>
            <option value="Malaysia">Malaysia</option>


    <option value="Maldives">Maldives</option>
    <option value="Mongolia">Mongolia</option>
    <option value="Myanmar">Myanmar (Burma)</option>
    <option value="Pakistan">Pakistan</option>
    <option value="Philippines">Philippines</option>
    <option value="Saudi Arabia">Saudi Arabia</option>
    <option value="Singapore">Singapore</option>
    <option value="Sri Lanka">Sri Lanka</option>
    <option value="Syria">Syria</option>
    <option value="UAE">United Arab Emirates</option>
    <option value="Uzbekistan">Uzbekistan</option>

        </select> 

        </tr>
        <tr>
          <td valign="top" align="right">
    Tel. no.:</td>
          <td valign="top" align="left">
    <input
     type="text" size="20" name="telno" placeholder="###-####"></td></tr>
     <tr>
          <td valign="top" align="right">
    Occupation:</td>
          <td valign="top" align="left">
    <input
     type="text" size="20" name="occ" ></td></tr>
     <tr>
          <td align="right">
    Spouse:</td>
          <td >
    <input
     type="text" size="20" name="spo" ></td>
      <td align="right">
    Occupation:</td>
          <td >
    <input
     type="text" size="20" name="occ2" ></td></tr>



     <tr>
          <td align="right" width="20">
    Contact Person(in case of emergency):</td>
          <td >
    <input
     type="text" size="20" name="cp" ></td>
      <td align="right">
    Tel. no:</td>
          <td >
    <input
     type="text" size="20" name="tel2" ></td></tr>


      <tr>
          <td valign="top" align="right" nowrap>
    Address:</td>
          <td valign="top" align="left">
    <input type="text" name="address"  size="50"></td>
          </tr>

          <tr>
          <td valign="top" align="right" nowrap>
    Reffered By:</td>
          <td valign="top" align="left">
    <input type="text" name="reff" size="31"></td>
          </tr>

        <tr>
         <td class="topHeadrow5" valign="top" width="200" colspan="5">
    <b><font size="4">
    Past Medical History:</font></b></td>
          </tr>

    <tr>
          <td valign="top" align="right" nowrap>
    Allergy:</td>
          <td valign="top" align="left">
    <input type="text" name="all" size="31"></td>
          </tr> 
          <tr>
          <td valign="top" align="right" nowrap>
    Previous illness:</td>
          <td valign="top" align="left">
    <input type="text" name="ill" size="31"></td>
          </tr>

        <tr>
         <td class="topHeadrow5" valign="top" width="100%" colspan="5">
    <b><font size="4">
    Heredo-Familial Diseases:</font></b></td>
          </tr>
        <tr>
          <td valign="top" align="right" nowrap>
    Maternal:
         </td>
          <td valign="top" align="left">
    <input type="text" name="mat" size="20"></td>
          </tr>
        <tr>
          <td valign="top" align="right" nowrap>
    Fraternal:
         </td>
          <td valign="top" align="left">
    <input type="text" name="frat" size="20"></td>
          </tr>

    <tr>
         <td class="topHeadrow5" valign="top" width="100%" colspan="5">
    <b><font size="4">
    Examination:</font></b></td>
          </tr>   
    <tr>
          <td valign="top" align="right" nowrap>
    Date / Time:</td>
          <td valign="top" align="left">
    <input type="text" name="datetime" size="31"></td>
          </tr>
          <tr>
          <td valign="top" align="right" nowrap>
    Vital Signs:</td>
          <td valign="top" align="left">
    <input type="text" name="vs" size="31"></td>
          </tr>
          <tr>
          <td align="center" nowrap>
    Complain PPE Findings:</td>
          <td valign="top" align="left">
    <input type="text" name="ppe" size="31"></td>
          </tr>
           <tr>
          <td align="center" nowrap>
    Plan / Management / FF-UP</td>
          <td valign="top" align="left">
    <input type="text" name="ffup" size="31"></td>
          </tr>
        <tr>
          <td width="10%">&nbsp;</td>
          <td width="90%"><input class="form-button" type="Submit" name="submit" value="Enter information"></td>
        </tr>
      </table>
      </form>
    <p>&nbsp;</p>

    <?
    }

    ?>

以上代码的结果在上面的屏幕截图中。我注释掉了`include('testinclude.php')。但是,一旦我包含该文件,患者信息中的所有数据都将消失,但程序列看起来已经消失。我希望他们两个出现。

当我包含testinclude.php时。这是结果:

enter image description here

testinclude.php

<?

$id = '17';
//printf("<td align=left><font face=Verdana size=1 color=red>There are no Records</font></td>",$num); //prints number

$procedures = "SELECT * FROM incurredcharges WHERE patient_no = '$id'";
$result = mysql_query($procedures ,$dbh2);


    if (mysql_num_rows($result) >= 1){


            $procedures = "SELECT
            `incurredcharges`.`procedure_no`,
  `c`.`procedure`

FROM
  incurredcharges
  INNER JOIN (
    SELECT `procedure`, `procedure_no` FROM `charges`
    UNION ALL
    SELECT `confinement`, `procedure_no` FROM `confinement`
    UNION ALL
    SELECT `service`, `procedure_no` FROM `ultrasound`
  ) c ON `incurredcharges`.`procedure_no` = c.`procedure_no`
WHERE `incurredcharges`.`patient_no` = '$id'";

            $result2 = mysql_query($procedures ,$dbh2);


            echo "<p><table border=1 cellspacing=0 cellpadding=2 bordercolor=#000000 width=100%>";
            echo "<tr align=top>
            <td class=topHeadrow5><b>Procedure #</b></td>
            <td class=topHeadrow5><b>Charge Incurred</b></td></tr>";


        while($row = mysql_fetch_array($result2)){

                    echo '
                    <tr>
                        <td>'.$row[0].'</td>
                        <td>'.$row[1].'</td>'; 
                        echo "</tr>";

        }
        echo '</table>';
        }
    else {

        echo "<p><table border=1 cellspacing=0 cellpadding=2 bordercolor=#000000 width=100%>";
        echo "<tr align=top>
        <td class=topHeadrow4><b>There are no Records</b></td></tr></table>";
        return false;
        }
?>

我已经在连接多个数据库时使用了这个技巧。每mysql_query,我都指定了它对应的dtabase配置。但它仍然无法奏效。

编辑:

好的,我只是尝试在底部移动include('testinclude.php')。并且它工作了。它显示了患者信息和程序..

但在我的官方代码中,它没有。

3 个答案:

答案 0 :(得分:1)

您必须为每个数据库创建link并将该链接传递给查询

 $link1 = mysql_select_db('invoices', $dbh1);
 $link2 = mysql_select_db('hmis', $dbh2);


 $sql1 = mysql_query($query1,$link1);
 $sql2 = mysql_query($query2,$link2);

答案 1 :(得分:1)

请参阅以下代码,

$dbh1 = mysql_connect($hostname, $username, $password); 
$dbh2 = mysql_connect($hostname, $username, $password, true); 

mysql_select_db('database1', $dbh1);
mysql_select_db('database2', $dbh2);

Then to query database 1, do this:

mysql_query('select * from tablename', $dbh1);

and for database 2:

mysql_query('select * from tablename', $dbh2);

答案 2 :(得分:0)

在打开第二个MySql连接时,您需要将第5个参数传递为true。

请参阅 MySql 手册,

resource mysql_connect (string $server, string $username,   
string $password, bool $new_link = false )

<强> new_link
如果使用相同的参数对mysql_connect()进行第二次调用,则不会建立新的链接,而是返回已打开的链接的链接标识符。 new_link参数修改了这种行为,并使mysql_connect()始终打开一个新的链接,即使之前使用相同的参数调用了mysql_connect()。在SQL安全模式下,将忽略此参数。

我希望这会有所帮助。