将mysqli转换为pdo准备好的语句

时间:2019-01-11 19:51:28

标签: php mysql post

我无法获得我的pdo准备好的语句代码来提取记录!仅当我用MySQLi代码编写时。  带有以下两个代码的filter_month.php。

filter_month.php -------- PDO-Conversion ---防止SQL注入不起作用!

<?php
    {

        include 'db_connection2.php';

        $query = " SELECT 
  g.name as `group`,
  COUNT(ar.present) as attended



 FROM 
   attendance_record ar

   INNER JOIN
   _person p
   ON ar.personid = p.id

   INNER JOIN
   _person_group g 
   ON ar.groupid = g.id


-- WHERE

AND
year(date) = ? AND month(date) = ?
AND
ar.present = 1

GROUP BY g.name
ORDER BY ar.date, g.name ASC
                 ";

$stmt = $pdo->prepare($query);
$stmt->execute([$_POST["year"]],[$_POST["month"]]);
$stmt->fetchAll(PDO::FETCH_ASSOC);

     //-----------------------------Table------------------------------------// 

        $output .= '  
           <table class="table table-bordered"> 


                   <tr>  
                               <th style="text-align:center;" width=".001%"><font size=2><span>Class</span></th>
                              <th style="text-align:center;" width=".001%"><font size=2><span>Attended</span></th>
                </tr>  

                     ';

                foreach($stmt as $row)

            {



 $output .= '  
                     <tr>  
                 <td style="text-align:center;">' . $row['group'] . '</td>
                 <td style="text-align:center;">' . $row['attended'] . '</td>
                     </tr>  
       ';
            }

        $output .= '</table>';    }

   $pdo=null;
    // By this way you can close connection in PDO.    
 ?>

filter_month.php ----- mysqli -----此代码有效!

<?php

    {
        include 'db_connection.php';


        $query = " SELECT 
  g.name as `group`,
  COUNT(ar.present) as attended



 FROM 
   attendance_record ar

   INNER JOIN
   _person p
   ON ar.personid = p.id

   INNER JOIN
   _person_group g 
   ON ar.groupid = g.id

-- WHERE
AND
YEAR(date) = '".$_POST["year"]."'
AND
Month(date) = '".$_POST["month"]."'
AND
ar.present = 1

GROUP BY g.name
ORDER BY ar.date, g.name ASC
                 ";


        $result = mysqli_query($conn, $query);

        $conn->close();

  //-----------------------------Table------------------------------------// 
        $output .= '  
           <table class="table table-bordered">  



                     <tr>  
                              <th style="text-align:center;" width=".001%"><font size=2><span>Class</span></th>

                              <th style="text-align:center;" width=".02%"><font size=2><span>Attended</span></th>
                    </tr>  

                     ';

                  while($row = mysqli_fetch_array($result))

            {


                $output .= '  
                     <tr>  
                           <td style="text-align:center;">' . $row['group'] . '</td>
                           <td style="text-align:center;">' . $row['attended'] . '</td>
                     </tr>  
                ';

        }
        $output .= '</table>';
        echo $output;
    }

 ?>

我尝试了许多不同的方式来编写代码,但是无法获取记录。刚刚了解pdo。 还尝试添加客户端reportmonthpage.php的图像,但无法弄清楚如何发布图像。

这只是我的filter_year.php,而不是年月,因此可以正常工作。如果我按照您的建议去掉了括号,它将不再提取记录。

<?php

    {
        include 'db_connection2.php';        
   $query = "SELECT         
  g.name as `group`,
  COUNT(ar.present) as attended
 FROM 
   attendance_record ar

   INNER JOIN
   _person p
   ON ar.personid = p.id

   INNER JOIN
   _person_group g 
   ON ar.groupid = g.id  

-- WHERE
AND
YEAR(date) = ?
AND
ar.present = 1
 ";

$stmt = $pdo->prepare($query);
$stmt->execute([$_POST["year"]]);
$result = $query;

  $output .= '  
           <table class="table table-bordered"> 

                   <tr>  
                              <th style="text-align:center;" width=".001%"><font size=2><span>Total Year Attendance</span></th>
                </tr>  

                     ';                 

            foreach($stmt as $row)

            {           


 $output .= '  
                     <tr>  
                 <td style="text-align:center;">' . $row['attended'] . '</td>
                     </tr>  
       ';
            }

        $output .= '</table>'; 
        }

  $pdo=null;
    // By this way you can close connection in PDO.   
 ?>

2 个答案:

答案 0 :(得分:2)

您想要在execute函数中使用一个数组,并且以没有数组的方式放错了括号。

[$_POST["year"]],[$_POST["month"]]应该为[$_POST["year"],$_POST["month"]]才能创建数组。您的括号过多。

答案 1 :(得分:-2)

这是我想出的方法!我敢肯定这一定是错的,但是可以!

<?php
    {

        include 'db_connection2.php';

        $query = " SELECT 
  g.name as `group`,
  COUNT(ar.present) as attended

 FROM 
   attendance_record ar

   INNER JOIN
   _person p
   ON ar.personid = p.id

   INNER JOIN
   _person_group g 
   ON ar.groupid = g.id


-- WHERE

AND
month(date) = ? AND year(date) = ?
AND
ar.present = 1

GROUP BY g.name
ORDER BY ar.date, g.name ASC
                 ";

$stmt = $pdo->prepare($query);
$stmt->execute([$_POST["month"],$_POST["year"]] );
$result = $query;


     //-----------------------------Table------------------------------------// 

        $output .= '  
           <table class="table table-bordered"> 

    <div align="center"><font size=4>
    Total present (by Class)-------PDO------ Not Working Code</font>
    </div>

                   <tr>  
                               <th style="text-align:center;" width=".001%"><font size=2><span>Class</span></th>
                              <th style="text-align:center;" width=".001%"><font size=2><span>Attended</span></th>
                </tr>  

                     ';

                foreach($stmt as $row)

            {



 $output .= '  
                     <tr>  
                 <td style="text-align:center;">' . $row['group'] . '</td>
                 <td style="text-align:center;">' . $row['attended'] . '</td>
                     </tr>  
       ';
            }

        $output .= '</table>';    }

   $pdo=null;
    // By this way you can close connection in PDO.    
 ?>

谢谢大家的光临!