2013-02-08 145 views
1

enter image description here連接2個不同的數據庫,不能正常工作

首先,我已經知道的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配置。但它仍然無法正常工作。

編輯:

OK我只是想移動包括( 'testinclude.php')在底部。和它工作。它顯示患者信息和程序..

但在我的官方代碼,它doesnt。

回答

1

你必須創建link每個數據庫,並通過該鏈接查詢

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


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

那就是我所做的。我看到每一個mysql_query,我添加了相應的數據庫連接,如'$ result = mysql_query($ sql,$ dbh1);' – user1967153

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

我在兩個連接上添加了pconnect並且它仍然是相同的 – user1967153

+0

eidted代碼請檢查出 –

+0

正是我所做的。也許關於很長的代碼不會使它工作。但我嘗試了一個簡單的代碼,它的工作原理。但在我的它並沒有。請查看我發佈的代碼。每一個mysql_query,我都指定了connecton。 – user1967153

0

當打開第二MySQL連接,你需要通過5說法這是真的。

請參閱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安全模式下,該參數被忽略。

我希望這會有所幫助。