Reputation: 3225
i am using this code to display a full MySQL Table:
$sql="SELECT * FROM billing ";
$rs=mysql_query($sql,$conn);
$fields_num = mysql_num_fields($rs);
echo "<table border='1'><tr>";
for($i=0; $i<$fields_num; $i++) {
$field = mysql_fetch_field($rs);
echo "<td>{$field->name}</td>";
}
echo "</tr>\n";
while($row = mysql_fetch_row($rs)) {
echo "<tr>";
echo "<td>$row[0]</td>";
echo "<td>$row[1]</td>";
echo "</tr>\n";
}
this is showing every column heading but only 2 columns data
how can i make it show all columns without having to list each column name/array number?
Upvotes: 0
Views: 157
Reputation: 2564
You could try this:
$sql="SELECT * FROM billing ";
$rs=mysql_query($sql,$conn);
$fields_num = mysql_num_fields($rs);
echo "<table border='1'><tr>";
for($i=0; $i<$fields_num; $i++) {
$field = mysql_fetch_field($rs);
echo "<td>{$field->name}</td>";
}
echo "</tr>\n";
$numrows = mysql_num_rows($rs);
while($row = mysql_fetch_row($rs)) {
$i = 0;
while($i < $numrows){
echo "<tr>";
echo "<td>".$row[$i]."</td>";
echo "</tr>\n";
$i++;
}
}
Upvotes: 0
Reputation: 2238
Just iterate though all columns in each row.
$sql="SELECT * FROM billing ";
$rs=mysql_query($sql,$conn);
$fields_num = mysql_num_fields($rs);
echo "<table border='1'><tr>";
for($i=0; $i<$fields_num; $i++) {
$field = mysql_fetch_field($rs);
echo "<td>{$field->name}</td>";
}
echo "</tr>\n";
while($row = mysql_fetch_row($rs)) {
echo "<tr>";
foreach($row as $index => $data) {
echo "<td>".$data."</td>";
}
echo "</tr>\n";
}
Upvotes: 1
Reputation: 1138
while($row = mysql_fetch_row($rs)) {
echo "<tr>";
for($i=0; $i<$fields_num; $i++) {
echo "<td>{$row[$i]}</td>";
}
echo "</tr>\n";
}
Upvotes: 0
Reputation: 704
Use mysql_fetch_array($rs)
instead of mysql_fetch_row($rs) and access each row of the resultset $rs
by using the field names called above which will simplifies your query..
Try so..
Upvotes: 3