PHP: mysql_num_fields - Manual

downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

mysql_num_rows mysql_list_tables

Last updated: Fri, 26 Jan 2007

view this page in

# mysql_num_fields

(PHP 3, PHP 4, PHP 5)

mysql_num_fields -- Возвращает количество полей результата запроса

## Описание

int mysql_num_fields ( resource result )

mysql_num_fields() возвращает количество полей результата запрооса `result`.

См. также mysql_select_db(), mysql_query(), mysql_fetch_field() и mysql_num_rows().

Для совместимости, как алиас, доступна устаревшая функция mysql_numfields(). Однако, использовать её крайне не рекомендуется.

add a note User Contributed Notes
mysql_num_fields
tharkey at tharkey dot net
19-Jun-2003 11:26
`You can use it without a requete, just to list the fields :
$liste_champs = mysql_list_fields ( $Base, $Table, $connexion);
for ($i=0; $i < mysql_num_fields ($l_champs); $i++) {
echo ( mysql_field_name ($l_champs, $i) );
echo (' / ');
}`
apass AT passmoore DOT com
01-Oct-2002 03:52
`Adding to the last comment: you can dynamically loop through any number of columns AND rows like so-
$query="your SQL";
$result=mysql_query($query) or die("Query ($query) sucks!");
$fields=mysql_num_fields($result);
echo "<table>\n<tr>";
for ($i=0; $i < mysql_num_fields($result); $i++) //Table Header
{ print "<th>".mysql_field_name($result, $i)."</th>"; }
echo "</tr>\n";
while ($row = mysql_fetch_row($result)) { //Table body
echo "<tr>";
for ($f=0; $f < $fields; $f++) {
echo "<td>$row[$f]</td>"; }
echo "</tr>\n";}
echo "</table><p>";
This has been tested.`
matt at iwdt dot net
23-Sep-2001 06:09
`here's one way to print out a row of <th> tags from a table
NOTE: i didn't test this
$result = mysql_query("select * from table");
for ($i = 0; $i < mysql_num_fields($result); $i++) {
print "<th>".mysql_field_name($result, $i)."</th>\n";
}
post a comment if there's an error`
bwark at stanford dot edu
24-Dec-2000 02:56
`If you just want the number of fields in a table, you can do something like this:
--
$db_id = mysql_connet();
$result = mysql_query("DESCRIBE [tableName], $db_id);
$numFields = mysql_num_rows($result);
--
Because "DESCRIBE" returns one row for each field in the table (at least in MySQL), this will work.`
dubious at 2xtreme dot net
13-Sep-2000 02:35
`Examples? How about this for a highly contrived but hopefully understandable example:
------------------------
<?php
function DoAQuery($Query)
{
$Result=mysql_db_query($Query);
$NumberOfFields=mysql_num_fields($Result);
print("There are $NumberOfFields Columns in the result<br>");
}
$ConnectionResource=mysql_connect("localhost","User","Password");
$Query="SELECT This, That, TheOther from Some_Table ORDER BY This";
DoAQuery($Query);
?>
-------------------------
Presuming I did that right, the entire output of this snippet should be "There are 3 Columns in the result" (assuming you actually have a mysql database with those fields, etc.) Presumably, in the Real World, you'd be using mysql_num_fields for formatting and such.
Hey, I SAID it was highly contrived, didn't I?`
add a note
mysql_num_rows mysql_list_tables

Last updated: Fri, 26 Jan 2007
show source | credits | stats | sitemap | contact | advertising | mirror sites
Copyright © 2001-2007 The PHP Group
All rights reserved.
This mirror generously provided by: Yahoo! Inc.
Last updated: Sat Mar 10 10:36:09 2007 PST