Home > Net >  Select columns only show The first column
Select columns only show The first column

Time:12-26

does anyone have any idea why I only get the first column printed with this code

stmt = $db->prepare('SELECT column_name
FROM information_schema.columns
WHERE  table_name = "players"
   AND table_schema = "hmanager" ');
        $stmt->execute();
        $row = $stmt->fetch(PDO::FETCH_ASSOC);
        
echo var_dump($row);

foreach ($row as $colnanme) {
  echo "$colnanme<br>";
}

CodePudding user response:

You have to run $stmt->fetch in while loop to show all columns.

<?php
    $stmt = $db->prepare("SELECT column_name FROM 
                          information_schema.columns
                          WHERE  table_name = 'players'
                          AND table_schema = 'hmanager' ");
    $stmt->execute();                
    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
      echo $row['column_name']. "</br>";
    }

?>
  • Related