简体   繁体   中英

Convert mysqli to pdo prepared statement

I cant get my pdo prepared statement code to pull the records! Only if I write it in MySQLi code. filter_month.php with both codes below.

filter_month.php--------PDO-Conversion---Prevent SQL Injection Not working!

<?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-----This code works!

<?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;
    }

 ?>

I have tried many different ways to write the code but, cant get to pull records. Just now learning about pdo. Also trying to add an image of the client side reportmonthpage.php but cant figure out how to post image.

Here is just my filter_year.php, not year and month, and it works. If I remove the brackets as you have suggested it wont pull records any longer.

<?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.   
 ?>

You want an array in the execute function and you've misplace brackets in such a way that you do not have an array.

[$_POST["year"]],[$_POST["month"]] should be [$_POST["year"],$_POST["month"]] to create the array. You have too many brackets.

Here is what I came up with that works! I'm sure it must be wrong, but it works!

<?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.    
 ?>

Thank you all for your time!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM