Automating output of MySQL rows in PHP
Continuing from my other question, I now know how to extract the fieldnames into variables with the result stored in them.
However, lets say I wish to output like this:
<?php
echo "Here is field1: ".$row['field1'];
echo "Here is field2: ".$row['field2'];
echo "Here is field3: ".$row['field3'];
?>
And I have over 40 fields in my table, so to avoid having to type them 开发者_如何学编程all out like the above, how can I automate it?
foreach ($row as $key => $value) {
echo "Here is '" . $key . "': " . $value . "<br>\n";
}
<?php
for($i = 1; $i < 40; $i++) {
echo "Here is field" . $i . ": ".$row['field' . $i];
}
?>
But I wonder, why do you have 40 columns in your table? Seems like you should redesign it.
foreach ($row as $key => $value) {
echo "Here is ".$key.": ".$value;
}
Or I don't get it:)
精彩评论