Advertisement
Guest User

PHP Code gone wrong

a guest
Jun 20th, 2019
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.27 KB | None | 0 0
  1. <?php
  2.   try {
  3.   $con= new PDO('mysql:Localhost;dbname=DB_Name', "username", "password");
  4.   $con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  5.   $query =
  6.   "SELECT vehicle_plate as 'Plate',
  7.  vehicle_color as 'Color',
  8.  vehicle_model as 'Model',
  9.  vehicle_ownername as 'Owner Name',
  10.  vehicle_impoundedTill as 'Release Date'
  11.  FROM vehicles where vehicle_isImpounded = true ";
  12.  //SQL query only shows if cars are Impounded
  13.  
  14.   //first pass just gets the column names
  15.   print "<table> ";
  16.   $result = $con->query($query);
  17.  
  18.  
  19.  
  20.   //return only the first row (we only need field names)
  21.   $row = $result->fetch(PDO::FETCH_ASSOC);
  22.   if ($row > 0){
  23.         print " <tr> ";
  24.         foreach ($row as $field => $value){
  25.         print " <th>$field</th> ";
  26.                 } // end foreach
  27.             print " </tr> ";
  28.             //second query gets the data
  29.             $data = $con->query($query);
  30.             $data->setFetchMode(PDO::FETCH_ASSOC);
  31.             foreach($data as $row){
  32.                 print " <tr> ";
  33.                     foreach ($row as $name=>$value){
  34.                     print " <td>$value</td> ";
  35.                     } // end field loop
  36.                 print " </tr> ";
  37.             } // end record loop
  38.             print "</table> ";
  39.  
  40.         else{
  41.             echo 'There are no cars impunded'
  42.             }
  43.     } //end if
  44.   } // end try
  45.   catch(PDOException $e) {
  46.    echo 'ERROR: ' . $e->getMessage();
  47.   }
  48.  ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement