gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 06:52:02 25 4
gpt4 key购买 nike

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,我都指定了它对应的数据库配置。但它仍然不起作用。

编辑:

好的,我只是尝试将 include('testinclude.php') 移动到底部。它奏效了。它显示了患者信息和程序..

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

最佳答案

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

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


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

关于php - 连接 2 个不同的数据库不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14767455/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com