2016-05-06 137 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 
} 

通過這種方式,查詢首先被髮送到數據庫服務器和值都單獨發送。