$result = mysql_query("select * from formtable") or die("Records could not be fetched");
while($row=mysql_fetch_row($result))
{
echo $row[0].$row[1].$row[2];
}
The above code runs perfect. But i find this statement -> echo $row[0].$row[1].$row[2]
bit too static. Because if a new column is added i will have to modify the code as echo $row[0].$row[1].$row[2].$row[3]
.
So, How can i count total columns of the row in order to avoid such a problem?
You can use count($row) to find the number of fields. But it is better to use a foreach loop as previously suggested.
Better to use something like:-
foreach($row AS $column)
{
echo $column;
}
mysql_num_fields() is what you're looking for.
Just to not leave it unspoken; the mysql_*
family functions are deprecated, for new code you should use mysqli
or PDO
.
Wouldn't it make more sense to use something like this:
echo implode($row);
No need to use a loop when there's a function that does it for you...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.