1

すべての mysql_* を PDO にアップグレードしています。単純な SELECT と INSERT を処理できます。しかし、ネストされたループを使用する複雑な SELECT が 1 つあります。mysql_* コードを投稿します。このコードは、2 列のテーブルを出力し、最初の列にA1 レベルでの合計登録数、2 番目の列に括弧内に支払ったA1 レベルでの合計登録数を示します。

-------------------------
Smithsville : A1 |      |
  10             | (7)  |
-------------------------
Grange : A1      |      |
   4             |  (4) |
-------------------------
Beau Ridge : A1  |      |
   23            |  (16)|
-------------------------
Jonesboro : A1   |      |
   9             | (9)  |
-------------------------
Lexing : A1      |      |
  3              | (1)  |
-------------------------

完全なアプリケーションでは、他のレベルのページ全体にさらにテーブルが生成されます。

$levels = array('A1', 'A2', 'B1', 'B2');

$centres = array('Smithsville', 'Grange', 'Plateau Ridge', 'Jonesboro', 'Lexing');

for ($j = 0; $j < 1; $j++)  /* Does not loop; selects only A1 in queries below*/
    {
       for ($k=0; $k<5; $k++) /* Cycles through the centres */
         {
        $show_count = mysql_query("SELECT COUNT(Centre) 
                                          FROM exam 
                    WHERE exam.Centre='".$centres[$k]."' 
                    AND exam.Level='".$levels[$j]."'");

        $show_paid = mysql_query("SELECT COUNT(Centre) 
                       FROM exam
                       JOIN personal 
                       ON exam.P_ID=personal.P_ID
                       WHERE personal.Paid='1' 
                       AND exam.Centre='".$centres[$k]."'
                       AND exam.Level='".$levels[$j]."'");


    $result_rows = mysql_num_rows($show_count);

    $result_rows2 = mysql_num_rows($show_paid);

        if ($result_rows == 0) 
           { echo 'No registrations';}

              if ($result_rows2 == 0) 
           { echo 'No payments';}


        echo '<table class="Font3White">
            <tr class="Title">
              <td class="width8">'.$centres[$k].' : '.$levels[$j].'</td>
            <tr>';
while (($row = mysql_fetch_row($show_count)) && ($row2 = mysql_fetch_row($show_paid)))
          { 
             foreach ($row as $key => $value)  
            {
              echo '<td>';
              echo $value;  
                   echo '</td>';
            }

             foreach ($row2 as $key2 => $value2)
            {
                   echo '<td> (';
              echo $value2;  
              echo ')</td>';
            }

             echo '</tr>';
              }
        echo '</table>';
            }

        }

私のPDOの試みは

    for ($j = 0; $j < 1; $j++)  /* Does not loop; selects only A1 */
            {
                        for ($k=0; $k<5; $k++) /* Cycles through the centres */
                    {

            /*//////////////////////////*/
            /*   Prepares statements    */
            /*//////////////////////////*/

        $stmt1 = $db->query("SELECT COUNT(Centre) 
                FROM exam 
                WHERE exam.Centre=:centre   
                AND exam.Level=:level");

        $stmt2 = $db->query("SELECT COUNT(Centre) 
                FROM exam
                JOIN personal 
                ON exam.P_ID=personal.P_ID
                     WHERE personal.Paid='1' 
                AND exam.Centre=:centre 
                AND exam.Level=:level");

        /*/////////////////////*/
        /*   Binds parameters  */
        /*/////////////////////*/

    $stmt1->bindParam(':centre', $centre, PDO::PARAM_STR);
    $stmt1->bindParam(':level', $level, PDO::PARAM_STR);

    $stmt2->bindParam(':centre', $centre, PDO::PARAM_STR);
    $stmt2->bindParam(':level', $level, PDO::PARAM_STR);



    /*//////////////////////////*/
    /*   Executes statements    */
    /*//////////////////////////*/

    $stmt1->execute(array($centre, $level));
    $stmt2->execute(array($centre, $level));


    echo '<table class="Font3White">
            <tr class="Title">
              <td class="width8">'.$centres[$k].' > '.$levels[$j].'</td>
            <tr>';
   while (($row = $stmt1->fetch(PDO::FETCH_ASSOC)) && ($row2 = $stmt2->fetch(PDO::FETCH_ASSOC)))
        { 
           foreach ($row as $key => $value)  
                {
                  echo '<td>';
                  echo $value;  
                       echo '</td>';
                }

                 foreach ($row2 as $key2 => $value2)
                {
                       echo '<td> (';
                  echo $value2;  
                  echo ')</td>';
                }
                echo '</tr>';
        }
    echo '</table>';
        }

    }

私はこれがひどいことを知っていますが、うまくいくことを願っています。

4

2 に答える 2

1

あなたに汚い仕事をさせるのは間違っていることは知っていますが、それだけです。

あなたのコードにいくつかの変更を加えたので、私のコードとあなたのコードを比較してください。

お役に立てば幸いです。

$levels = array('A1', 'A2', 'B1', 'B2');

$centres = array('Smithsville', 'Grange', 'Plateau Ridge', 'Jonesboro', 'Lexing');

foreach ($levels as $level) {
    foreach ($centres as $centre) {

        // As you said, PREPARE
        $stmt1 = $db->prepare("SELECT COUNT(Centre) 
                FROM exam 
                WHERE exam.Centre=:centre   
                AND exam.Level=:level");

        $stmt2 = $db->prepare("SELECT COUNT(Centre) 
                FROM exam
                JOIN personal 
                ON exam.P_ID=personal.P_ID
                     WHERE personal.Paid='1' 
                AND exam.Centre=:centre 
                AND exam.Level=:level");

        $stmt1->bindParam(':centre', $centre, PDO::PARAM_STR);
        $stmt1->bindParam(':level', $level, PDO::PARAM_STR);

        $stmt2->bindParam(':centre', $centre, PDO::PARAM_STR);
        $stmt2->bindParam(':level', $level, PDO::PARAM_STR);

        //You don't need to pass parameters here just on PDOStatement::bindParam
        $stmt1->execute();
        $stmt2->execute();

        echo '<table class="Font3White">
            <tr class="Title">
              <td class="width8">' . $centre . ' > ' . $level . '</td>
            <tr>';

        // Fetch stuffs
        $row = $stmt1->fetch(PDO::FETCH_ASSOC);
        $row2 = $stmt2->fetch(PDO::FETCH_ASSOC);

        // Don't use while with PDOStatement::fetch, use fetchAll then foreach it.
        if ($row && $row2) {

            foreach ($row as $key /* <= Do you need it? */ => $value) {
                echo '<td>';
                echo $value;
                echo '</td>';
            }

            foreach ($row2 as $key2 /*  <= Do you need it? */ => $value2) {
                echo '<td> (';
                echo $value2;
                echo ')</td>';
            }

            echo '</tr>';
        }
        echo '</table>';
    }
}
于 2013-04-30T16:12:15.783 に答える