动态回显所有列名和所有行

时间:2015-02-01 17:57:18

标签: php mysql sql rows

哟兄弟们,我试图回应一个表中的所有数据,包括列名和字段。我能够成功获取列名,我也想获得所有行和字段,我不确定我是否采取正确的方法来解决我的问题,请帮助。

//this is to echo coloumn names
$table = '<table border="1" cellspacing="10" cellpadding="10" style="width:60%">';
$table2 = "</table>";
$sql2 = "SHOW COLUMNS FROM $tablename";
$query2 = mysqli_query($conn,$sql2);
$tablename = $_POST['db'];
echo $table;
while($row = mysqli_fetch_array($query2)){
echo $row[0] . "&nbsp;&nbsp;&nbsp;";
}
echo $table2 . "<br /><br />";
// successful in echoing coloumn names

// now i want to echo rows based on the number of coloumns
$countcol = mysqli_num_rows($query2);
$sql = "SELECT * FROM $tablename";
$query = mysqli_query($conn,$sql);

echo $table;
$i = "";
while($row=mysqli_fetch_array($query)){
for($i=0;$1<$countcol;$i++){
echo $row[0];
}
}
echo $table2 . "<br /><br />";
// this doenst get the all the rows based on the no of coloumns

1 个答案:

答案 0 :(得分:1)

在最后一个循环中,echo $ row [0]是正确的还是应该是$ row [$ i]