0

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?

1
  • if you know how many rows will be returned, you just put echo <td> in a while => while (index< numberOfRows) {echo "<td>$row[index]</td>"; index++;} Commented Jan 21, 2014 at 17:13

4 Answers 4

3

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..

Sign up to request clarification or add additional context in comments.

Comments

1

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";
}

4 Comments

perfect - thanks. if i added links to the column headings, how could i do it so it would be <a href="page.php?orderby=COL_HEADING&order=ASC"><td>{$field->name}</td></a> and then if that is the current page make it change to order=DESC
Use GET for the querystring parameters and then concatenate the SQL query with ORDER BY and ASC/DESC. Just string manipulation.
dont suppose you have an example please?
0
while($row = mysql_fetch_row($rs)) {
    echo "<tr>";
    for($i=0; $i<$fields_num; $i++) {
        echo "<td>{$row[$i]}</td>";
    }
    echo "</tr>\n";
}

Comments

0

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++;
    }
}

3 Comments

what? it will still pull the most recent $i when it gets to the second loop.
il you have 3 rows with 4 fields, $i will go up to 12, so it will crash when trying to access $row[5];
for futur reader : iamde_coder have moved the $i=0 in the first loop

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.