2016-05-06 142 views
1

大家好! 我是新来的编码,将在明年九月上课。期待那.... :-)PHP Myqsl PDO代码效率 - foreach循环内的Foreach循环

我正在为我的数据库做一个修改页面。它由输入和下拉列表组成,用于修改数据库的内容。至少现在......直到我学到更多。

我为自己编码,想知道以下是否是正确的方法?在我看来,它不是,因为每当外层循环经过时内层查询都会被执行......但是它工作!?!?

这是我能找到使内部foreach循环($ familylist)与MySQL查询工作的唯一途径。如果内循环的查询在外循环之外($ plantList)...它不起作用。第一个下拉列表会填充内容,但以下行不会,至少只有第一个选项,它不会填充查询中的内容。

任何帮助,欢迎和赞赏!

<?php //more code here.... 

$plantQuery = "SELECT id, genre, espece, famille FROM plante ORDER BY genre"; 

$plantList = $dbconnect->query ($plantQuery); 
?> 

<table> 
<thead> 
    <tr> 
    <th>ID</th> 
    <th>GENRE</th> 
    <th>ESPÈCE</th> 
    <th>FAMILLE</th> 
    </tr> 
</thead> 
<tbody> 
    <?php foreach ($plantList->fetchAll(PDO::FETCH_ASSOC) as $plant) : ?> 
     <form method="post"> 
     <tr> 
      <td><input name="id" value="<?php echo $plant["id"] ;?>" readonly></td> 
      <td><input name="genre" value="<?php echo $plant["genre"] ;?>"></td> 
      <td><input name="espece" value="<?php echo $plant["espece"] ;?>"></td> 
      <td><select name="familleList" > 
       <option value="0" >Choisir une famille</option> 
       <?php 
       $familyQuery = "SELECT id, famille FROM famille ORDER BY id ASC"; 
       $familyList = $dbconnect->query ($familyQuery); 
       ?> 
       <?php foreach ($familyList->fetchAll(PDO::FETCH_ASSOC) as $family):?> 
       <option value="<?php echo $family["id"] ;?>" <?php if ($plant["famille"] <> 0 && $plant["famille"] == $family["id"]) {echo "selected"; }?>><?php echo $family["id"] . " - " . $family["famille"] ;?></option> 
       <?php endforeach ;?> 
      </select>  
     </td> 
     <td><button name="modifier" type="submit">Modifier</button></td> 
     <td><button name="supprimer" type="submit">Supprimer</button></td> 
     </tr> 
    </form> 
    <?php endforeach ;?> 
</tbody> 
</table> 

回答

0

而不是多次调用数据库,对于loop中的相同数据。一个简单的解决方案是将其称为一次。在文件的顶部做这样的事情。

$families = $familyList->fetchAll(PDO::FETCH_ASSOC); 

然后你可以将你的foreach循环。

foreach ($families as $family) 

这将使查询执行一次,因此避免对database的多个查询。和中的已获取数据的loop

+0

显然我不记得基础!我需要拔下代码并取消代码。Merci! –

0

你说得对。你在做什么是非常低效的。数据库查询往往会造成瓶颈,所以最小化它们通常是最好的选择。

既然你不传递任何值到第二​​个查询,只是把它跳出循环:

<?php //more code here.... 

$plantQuery = "SELECT id, genre, espece, famille FROM plante ORDER BY genre"; 
$plantList = $dbconnect->query ($plantQuery); 

$familyQuery = "SELECT id, famille FROM famille ORDER BY id ASC"; 
$familyList = $dbconnect->query ($familyQuery); 
?> 

<table> 
<thead> 
<tr> 
    <th>ID</th> 
    <th>GENRE</th> 
    <th>ESPÈCE</th> 
    <th>FAMILLE</th> 
</tr> 
</thead> 
<tbody> 
    <?php foreach ($plantList->fetchAll(PDO::FETCH_ASSOC) as $plant) : ?> 
    <form method="post"> 
     <tr> 
     <td><input name="id" value="<?php echo $plant["id"] ;?>" readonly></td> 
     <td><input name="genre" value="<?php echo $plant["genre"] ;?>"></td> 
     <td><input name="espece" value="<?php echo $plant["espece"] ;?>"></td> 
     <td><select name="familleList" > 
      <option value="0" >Choisir une famille</option> 
      <?php 

      ?> 
      <?php foreach ($familyList as $family):?> 
      <option value="<?php echo $family["id"] ;?>" <?php if ($plant["famille"] <> 0 && $plant["famille"] == $family["id"]) {echo "selected"; }?>><?php echo $family["id"] . " - " . $family["famille"] ;?></option> 
      <?php endforeach ;?> 
      </select>  
     </td> 
     <td><button name="modifier" type="submit">Modifier</button></td> 
     <td><button name="supprimer" type="submit">Supprimer</button></td> 
    </tr> 
    </form> 
<?php endforeach ;?> 
</tbody> 
</table> 

即使这这里不是这样的,只要你是只执行相同的查询多次改变的变量,你可以使用预处理语句:

$familyQuery = $dbconnect->prepare("SELECT * FROM famille 
    WHERE something = :s 
    AND somethingelse = :se ORDER BY id ASC"); 
foreach ($values as $val) { 
    $familyQuery->bindValue('s', $val); 
    $familyQuery->bindValue('se', somefunction($val)); 
    $familyQuery->execute(); 
    $results = $familyQuery->fetchAll(); 
    // Do something with the results 
} 

通过这种方式,查询首先被发送到数据库服务器和值都单独发送。