2012-04-06 23 views
0

我有一個用HTML編寫的網頁。我有一個是通過利用MySQL查詢數據庫中填充的下拉列表:在PHP/HTML網頁中執行多個MySQL查詢:只有第一個查詢運行

<SELECT NAME = "Participant" STYLE = "WIDTH: 187" TITLE="Begin typing participant last name for fast searching." required> 
<OPTION SELECTED VALUE = "">Select Participant...</OPTION> 
<?PHP 
    $allParticipants = getall_participants(); 
    foreach($allParticipants as &$value) { 
     $dt = date('Y-m-d'); 
     $val = $value->get_id(); 
     $optval = $dt.$val; 
     echo "<OPTION VALUE='",$optval,"'>"; 
     echo $value->get_first_name()," ",$value->get_last_name(); 
     echo "</OPTION>"; 
    } 
?> 
</SELECT> 

的getall_participants()看起來像:

function getall_participants() { 
connect(); 
$query = "SELECT * FROM dbParticipants ORDER BY last_name"; 
$result = mysql_query ($query); 
$theParticipant = array(); 
while ($result_row = mysql_fetch_assoc($result)) { 
    $theParticipant = new Participant($result_row['last_name'], 
         $result_row['first_name'], $result_row['address']); 
    $theParticipants[] = $theParticipant; 
} 
mysql_close(); 
return $theParticipants; 
} 

這同一頁上我有一個文本框是預填寫的另一個數據庫:

<?php 
    $dt = date('Y-m-d'); 
    $participants = getall_dbParticipantEntry_byDate($dt); 
    foreach($participants as &$value) { 
     $a = $a.$value.", "; 
    } 
    echo "<INPUT TYPE='text' NAME='Participants' STYLE='WIDTH:50px;' TITLE='Participants' "; 
    echo "VALUE='[", $a.' ', "]'/>"; 
?> 

這getall_dbParticipantEntry_byDate($日期)看起來像:

function getall_dbParticipantEntry_byDate($date) { 
connect(); 
$query = 'SELECT * FROM dbParticipantEntry WHERE date = "'.$date.'"'; 
$result = mysql_query ($query); 
$theParticipantEntry = array(); 
while ($result_row = mysql_fetch_assoc($result)) { 
    $theParticipantEntry = new ParticipantEntry($result_row['date'], $result_row['id'], $result_row['call_time'], 
    $result_row['result'], $result_row['notes']); 
    $theParticipantEntries[] = $theParticipantEntry->get_id(); 
} 
mysql_close(); 
return $theParticipantEntries; 
} 

但是,雖然這兩個函數都能單獨工作,但當它們都在同一個網頁上時(就像我的意思是),只有第一個函數運行。我通過切換它們進行測試。他們都完成了他們的指定任務,但只能在頁面上單獨完成。 我怎樣才能讓他們都運行並填充他們各自的領域?

非常感謝。

回答

1

嘗試按以下順序:

  1. 連接到MySQL服務器

  2. 做任務1

  3. 做任務2

  4. 關閉連接

對於我來說,它看起來像你已經關閉了mysqlconnection,然後再執行task2。

編輯:

也許你能做到這樣呢?

function f1() 
{ 
    $res = mysql_connect(...); 

    // .. do some queries .. 
    mysql_query($sql, $res); 

    mysql_close($res) 
} 

function f2() 
{ 
    $res = mysql_connect(...); 

    // .. do some queries .. 
    mysql_query($sql, $res); 

    mysql_close($res) 
} 

編輯:

從php.net:

使用多條鏈路連接到同一個數據庫(有相同的用戶名)時要小心。除非您在mysql_connect()中明確指定以創建新鏈接,否則它將返回已打開的鏈接。如果這將被mysql_close()關閉,它也會(顯然)關閉另一個連接,因爲鏈接是相同的。 由於在< = 4.3.6有一個沒有關閉連接的錯誤,但在修補程序> = 4.3.7後,我的所有應用程序都因爲一個腳本而崩潰做過這個。

+0

我關閉了連接,但後來我在下一個函數中重新連接。這還不夠嗎? – 2012-04-06 23:41:19

+0

你如何連接? – Kerwindena 2012-04-06 23:48:21

+0

通過「connect();」在PHP中。不重新連接到數據庫? – 2012-04-06 23:49:32

0

您在同一連接上運行它們。您需要存儲從mysql_connect返回的資源ID並將其傳遞給每個mysql方法(每個方法都使用它自己的相關資源)。

這麼說,我覺得現在是時候:

  1. 移動到更現代的東西喜歡的mysqli或PDO擴展。更好的API
  2. 在連接管理上使用某種抽象,最好是每個連接的DB管理類的一個實例。網上有很多例子,並且提供這樣的說明超出了本網站的範圍。