2012-04-06 108 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管理类的一个实例。网上有很多例子,并且提供这样的说明超出了本网站的范围。