PHP
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: Mon, 26 Nov 2007

view this page in

mysql_num_fields

(PHP 4, PHP 5, PECL mysql:1.0)

mysql_num_fields — 取得结果集中字段的数目

说明

int mysql_num_fields ( resource $result )

mysql_num_fields() 返回结果集中字段的数目。

参见 mysql_select_db()mysql_query()mysql_fetch_field()mysql_num_rows()

为向下兼容仍然可以使用 mysql_numfields(),但反对这样做。



mysql_num_rows> <mysql_list_tables
Last updated: Mon, 26 Nov 2007
 
add a note add a note User Contributed Notes
mysql_num_fields
Anonymous
13-Aug-2008 02:50
asdasd asdasdas asdlskmd  lasid a osiajd as soidjasodija  saoidjsdoiaj asdioajsd asdoijasd asoidj aosidja osidj oasij dasoji doasij dosaijdo isahdiashdasuhdi ashu diu shaiu dhasiuh ad.
php at jezusisheer dot nl
30-Mar-2007 03:10
Note that, if you want to get the amount of columns of a table and you're using the "SHOW COLUMNS FROM $table" query, you will have to use mysql_num_rows() instead of mysql_num_fields() on the result. This becomes logical when thinking about it, because the SHOW COLUMNS query returns a result with six columns (Field, Type, Null, Key, Default and Extra) and with a single row for every column found. If you'd count the number of fields, you'd always get 6. If you count the number of rows, you'll get the amount of columns found.
tharkey at tharkey dot net
20-Jun-2003 02: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
02-Oct-2002 06: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
24-Sep-2001 09: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 06:56
If you just want the number of fields in a table, you can do something like this:

<?php
$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.

mysql_num_rows> <mysql_list_tables
Last updated: Mon, 26 Nov 2007
 
 
show source | credits | sitemap | contact | advertising | mirror sites