【问题标题】:PHP Mysql PDO code efficiency - Foreach loop within a foreach loopPHP Mysql PDO 代码效率 - foreach 循环内的 foreach 循环
【发布时间】:2016-09-02 03:08:59
【问题描述】:

大家好! 我是编码新手,将在明年 9 月上课。期待……:-)

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

我正在为自己编码,想知道以下是否是正确的方法?在我看来不是,因为每次外部循环通过时都会执行内部查询......但它有效!?!?

这是使内部 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>  

【问题讨论】:

    标签: php mysql loops pdo foreach


    【解决方案1】:

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

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

    然后您就可以将foreach 循环到。

    foreach ($families as $family)
    

    这将使查询执行一次,从而避免多次查询database。和looploops 中已经获取的数据。

    【讨论】:

    • 显然我不记得基础知识了!我需要拔掉和解开代码。谢谢!
    【解决方案2】:

    你是对的。你在做什么是非常低效的。数据库查询往往是真正造成瓶颈的东西,因此将它们最小化通常是最好的做法。

    由于您没有将任何值传递给第二个查询,因此只需将其从循环中取出:

    <?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
    }
    

    这样查询首先发送到数据库服务器,值都是单独发送的。

    【讨论】:

      猜你喜欢
      • 2015-10-29
      • 2015-07-12
      • 2016-09-26
      • 2012-07-19
      • 2015-03-14
      • 1970-01-01
      • 2016-10-25
      • 2013-03-05
      • 1970-01-01
      相关资源
      最近更新 更多