Skorpius

show the entire MySQL table

Jun 12th, 2022 (edited)
670
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.91 KB | None | 0 0
  1. <?php
  2.  
  3.     try{
  4.     $pdo = NEW PDO('mysql:host=localhost;dbname=july2020project','root','password');
  5.     }catch(PDOException $e){
  6.         die($e->getMessage());
  7.     }
  8.    
  9.  
  10.     $result = $pdo->query("SELECT * FROM users");
  11.    
  12.     $all = $result->fetchAll();
  13.    
  14.     $col = $all[0];
  15.    
  16.     $columns = array();
  17.    
  18.     echo "<pre>";
  19.    
  20.    
  21.     foreach($col AS $key=>$value){
  22.        
  23.         if(is_string($key)){
  24.            
  25.             $columns[] = $key;
  26.  
  27.         }
  28.        
  29.     }
  30.    
  31.     echo "<table border='1'>";
  32.     foreach($columns AS $value){
  33.        
  34.         echo "<th>$value</th>";
  35.        
  36.     }
  37.    
  38.     for($x=0;$x<count($all);$x++){
  39.        
  40.         echo "<tr>";
  41.             for($y=0;$y<count($columns);$y++){
  42.                
  43.                 echo "<td>".$all[$x][$y]."</td>";
  44.                
  45.             }
  46.         echo "</tr>";
  47.        
  48.     }
  49.    
  50.    
  51.    
  52.        
  53. ?>
Add Comment
Please, Sign In to add comment