How to get column names from PDO's fetchAll result?

后端 未结 1 1034
独厮守ぢ
独厮守ぢ 2020-12-04 00:56

I create a code to display from a table

$sql = "SELECT * FROM table"; 
$stmt = $dbcon->prepare($sql); 
$stmt->execute();
$result = $stmt->f         


        
1条回答
  •  独厮守ぢ
    2020-12-04 01:26

    There are many duplicated questions but all answers are beyond any reason, offering complicated solutions involving running extra queries etc.

    While the solution is right here: when using fetchAll(), you already have all the column headers in the $result variable

    $headerNames = $result ? array_keys($result[0]) : [];
    

    now you can foreach over $coulmnNames to get the table header and the foreach over $result to display the results.

    0 讨论(0)
提交回复
热议问题