My code is given below. In which I can only print the table headings. I want the table whose name I entered in the database should show the entire table.
<?php $dataconn = mysqli_connect("localhost", "root", "", "databasename"); $sql = "SELECT * FROM employeedetails"; $res = mysqli_query($dataconn, $sql); $show = mysqli_fetch_all($res, MYSQLI_ASSOC); $col = $show[0]; $columns = array(); echo "<pre>"; foreach ($col as $key => $value) { if (is_string($key)) { $columns[] = $key; } } echo "<table border='1' cellpadding='10'>"; foreach ($columns as $value) { echo "<th>$value</th>"; } for ($x = 0; $x < count($show); $x++) { echo "$value"; }
Advertisement
Answer
On second foreach loop you are printing same variable which you have used for column name, so its printing the column name also for second loop, edited code below please check, to print table data you need to print result of query
<?php $dataconn = mysqli_connect("localhost", "root", "", "database"); $sql = "SELECT * FROM tablename"; $res = mysqli_query($dataconn, $sql); $show = mysqli_fetch_all($res, MYSQLI_ASSOC); $col = $show[0]; $columns = array(); echo "<pre>"; foreach ($col as $key => $value) { if (is_string($key)) { $columns[] = $key; } } echo "<table border='1' cellpadding='10'>"; echo "<tr>"; foreach ($columns as $value) { echo "<td>$value</td>"; } echo "</tr>"; foreach ($show as $tableData) { echo "<tr>"; foreach ($tableData as $key => $val) { echo "<td>$val</td>"; } echo "</tr>"; }