在一个页面中连接两个不同的数据库

时间:2013-02-07 16:43:20

标签: php mysql

enter image description here

第二栏,在程序中,它说没有记录。不应该那样。其结果来自另一个数据库的数据。我已经有了它的代码,如果我使用包含不同的php文件与另一个mysql_connect配置(来自另一个数据库),患者信息不会像在这里看到的那样被检索:

enter image description here

我知道如何通过存储两个连接的不同变量来进行多个连接。但是它让我很困惑,导致它在不同的包含文件中。

屏幕截图中的该页面为editclient.php,其中包含editclient_include_inv.php,其中包含“过程”部分的代码。

editclient.php

<?


include('header.php');

  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) 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);
//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);
//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",$db);
$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);
$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);
$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">
<?
include('editclient_include_inv.php');
?>

      </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>

<?
}

?>

editclient_include_inv.php

<?



if ($num < 1){

$mysql_hostname = "localhost";
$mysql_user = "root";
$mysql_password = "";
$mysql_database = "hmis";
$bd = mysql_connect($mysql_hostname, $mysql_user, $mysql_password) or die("Could not connect database");
mysql_select_db($mysql_database, $bd) or die("Could not select database");


//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);


    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);


            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;
        }
}


?>

第二个代码用于从另一个数据库检索的Procedures列。

2 个答案:

答案 0 :(得分:0)

您尚未发布任何代码,但从问题文本中,我可以看到您正在使用mysql_connect()连接到数据库。

这里要学习的第一件事是不使用mysql_xxx()函数。这有很多原因 - 首先,它们已被弃用,而且它们也是不安全的。

您应该使用PHP内置的另外两个API:mysqliPDO

但在这种情况下,不使用mysql函数的更好理由是它们更难以管理多个数据库连接。

另一方面,使用mysqliPDO管理多个数据库连接非常容易。

$db1 = new PDO('mysql:host=xxxxx,etc',$user1,$pass1);
$db2 = new PDO('mysql:host=xxxxx,etc',$user2,$pass2);

$db1->query('SELECT * FROM table_on_db1');
$db2->query('SELECT * FROM table_on_db2');

因此,您可以根据需要保留尽可能多的活动连接对象,并使用适合任何给定查询的连接对象。

您可以在此处详细了解mysqli API:http://www.php.net/manual/en/book.mysqli.php

关于此处的PDO API:http://php.net/manual/en/book.pdo.php

希望有所帮助。

答案 1 :(得分:0)

mysql_connect()返回您需要存储的连接标识符,然后传入所有mysql_ *函数。这是每个脚本维护多个数据库连接的方式。

但是,不推荐使用mysql_ *函数,您应该转移到PDO或mySQLi。这将有一个额外的好处,允许您初始化和存储单独的数据库对象,而不是跟踪连接标识符。即:

$db1 = new PDO('mysql:host=host1;dbname=db1', $user1, $pass1);
$db2 = new PDO('mysql:host=host2;dbname=db2', $user2, $pass2);