2016-03-27 5 views
0
<?php 
$query = "SELECT bobot FROM `record_result` WHERE `participantid` = $idParticipant AND `questionid` = 1"; 
$query1 = "SELECT bobot FROM `record_result` WHERE `participantid` = $idParticipant AND `questionid` = 2"; 
$comments = mysql_query($query); 
$comments1 = mysql_query($query1); 
while($row = mysql_fetch_array($comments, MYSQL_ASSOC)) { 
    $bobot = $row['bobot']; 
    $bobot = htmlspecialchars($row['bobot'],ENT_QUOTES); 
} 
while($row = mysql_fetch_array($comments1, MYSQL_ASSOC)) { 
    $bobot1 = $row['bobot']; 
    $bobot1 = htmlspecialchars($row['bobot'],ENT_QUOTES); 
} 
?> 

Ich möchte diesen Code kann bis 10 Schleifen. Ich hoffe, dass es nicht viele Variable, zB: $ query, $ query1, $ query2, ..., $ query10, $ Kommentare, $ comments1, $ comments2, ..., $ comments10, $ bobot, $ bobot1, $ bobot2, ..., $ bobot10. Jemand hilft mir, bitte ...Kann ich diesen Code in PHP mit for for oder while?

Antwort

0

Sie sind fast da. Aber ich muss erwähnen, dass Sie parametrisierte Abfragen mit prepared statements beginnen sollten, anstatt Ihre Abfragen manuell aufzubauen.

$id = 1; 
while($id <= 10) { 
    // construct your query 
    $query = "SELECT bobot FROM `record_result` WHERE `participantid` = $idParticipant AND `questionid` = $id"; 
    // execute and get results 
    $comments = mysql_query($query); 

    // iterate over records in result 
    while($row = mysql_fetch_array($comments, MYSQL_ASSOC)) { 
     $bobot = $row['bobot']; 
     $bobot = htmlspecialchars($row['bobot'],ENT_QUOTES); 
    } 

    // increment the id for next cycle through the loop 
    $id = $id + 1; 
} 
+0

Danke mir geholfen, aber ich fand immer noch eine Fehlermeldung "Warning: mysql_fetch_array() erwartet Parameter 1 in bestimmten Ressource, boolean zu sein" auf Skript „while ($ row = mysql_fetch_array ($ Kommentare, MYSQL_ASSOC)) " –

Verwandte Themen