2016-03-27 11 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); 
} 
?> 

私はこのコードを10までループすることができますようにしたい:私は多くの変数、ex:$ query、$ query1、$ query2、...、$ query10、$ comments、$ comments1、 $ comments2、...、$ comments10、$ bobot、$ bobot1、$ bobot2、...、$ bobot10である。誰か助けてください...forまたはwhileを使用してPHPでこのコードをループすることはできますか?

答えて

0

あなたはほとんどあります。しかし、クエリを手動で構築するのではなく、prepared statementsでパラメータ化されたクエリを使用してください。

$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

は私を助けてくれてありがとう、私はまだ検出されたエラー:($行=は、mysql_fetch_array($コメント、MYSQL_ASSOC)しなが​​ら、」スクリプトの「警告は、mysql_fetch_array()は、パラメータ1で与えられたリソース、boolean型であることを期待します」 ) " –