mysql_num_fields

(PHP 4, PHP 5)

mysql_num_fieldsGet number of fields in result

Warning

This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used. See also MySQL: choosing an API guide and related FAQ for more information. Alternatives to this function include:

說明

int mysql_num_fields ( resource $result )

Retrieves the number of fields from a query.

參數

result

回傳類型為 resource 的結果集。該結果集從 mysql_query() 的呼叫中得到。

回傳值

Returns the number of fields in the result set resource on success 或者在失敗時回傳 FALSE.

範例

Example #1 A mysql_num_fields() example

<?php
$result 
mysql_query("SELECT id,email FROM people WHERE id = '42'");
if (!
$result) {
    echo 
'Could not run query: ' mysql_error();
    exit;
}

/* returns 2 because id,email === two fields */
echo mysql_num_fields($result);
?>

註釋

Note:

為了保證向前相容,可以使用下面的別名,但不贊成使用它: mysql_numfields()

參見

add a note add a note

User Contributed Notes 5 notes

up
0
php at jezusisheer dot nl
17 years ago
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.
up
-3
matt at iwdt dot net
22 years ago
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
up
-5
tharkey at tharkey dot net
20 years ago
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 (' / ');
                        }
up
-5
bwark at stanford dot edu
23 years ago
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.
up
-11
apass AT passmoore DOT com
21 years ago
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.
To Top