/    Sign up×
Community /Pin to ProfileBookmark

MySQL Column Count in PHP

Hi guys,

Ive been tasked to create a website for an electrical engineers company and need a bit of help and advice on a problem.

The sites going to be database driven and so far runs on three pages.

The problem I have at the moment is I need to called the database columns on a page, but they are different for each product.

My idea was to use the mysqli_fetch($r, MYSQLI_NUM) and then loop through number 0 to the last one called back.

This is where I get stumpted could anyone tell me a good way or loop to do this?
Is there a command that can count the number of columns called so I can use this in the loop?

Thanks in advance.
Regards
Ryan

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@NogDogAug 30.2008 — If using MySQLi, the [url=http://www.php.net/manual/en/mysqli-result.field-count.php]result's $field_count[/url] property is all you need to get the number of fields.

However, if your intent is just to loop through all of them, there's really no need since you can just use a foreach loop:
[code=php]
while($row = $result->fetch_row())
{
echo "<tr>";
foreach($row as $value)
{
echo "<td>".$value."</td>";
}
echo "</tr>n";
}
[/code]
×

Success!

Help @ryanSchranz spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...