 |
mysql_field_name (PHP 3, PHP 4, PHP 5) mysql_field_name --
取得结果中指定字段的字段名
说明string mysql_field_name ( resource result, int field_index )
mysql_field_name()
返回指定字段索引的字段名。result
必须是一个合法的结果标识符,field_index
是该字段的数字偏移量。
注:
field_index 从 0 开始。
例如,第三个字段的索引值其实是 2,第四个字段的索引值是 3,以此类推。
例子 1. mysql_field_name() 例子
<?php /* The users table consists of three fields: * user_id * username * password. */ $link = mysql_connect('localhost', "mysql_user", "mysql_password"); $dbname = "mydb"; mysql_select_db($dbname, $link) or die("Could not set $dbname: " . mysql_error()); $res = mysql_query("select * from users", $link);
echo mysql_field_name($res, 0) . "\n"; echo mysql_field_name($res, 2); ?>
|
以上例子将产生如下输出:
|
为向下兼容仍然可以使用 mysql_fieldname(),但反对这样做。
jamesl at si-usa dot com
27-Jul-2006 03:11
i figured this little diddy,
if you are trying to get all the field names from your table, but don't feel like doing all that typing. here is this little snippit.
you just have to know what your field names are to call them
$query = "SELECT * FROM `your_table`";
$results = mysql_query($query);
$numberfields = mysql_num_fields($results);
$row = mysql_fetch_object($results);
for ($i=0; $i<$numberfields ; $i++ ) {
$var = mysql_field_name($results, $i);
$$var = $row->$var;
}
echo $fieldname;
cheers.
James
janezr at jcn dot si
19-Oct-2005 10:18
This is another variant of displaying all columns of a query result, but with a simplified while loop.
<?
$query="select * from user";
$result=mysql_query($query);
$numfields = mysql_num_fields($result);
echo "<table>\n<tr>";
for ($i=0; $i < $numfields; $i++) // Header
{ echo '<th>'.mysql_field_name($result, $i).'</th>'; }
echo "</tr>\n";
while ($row = mysql_fetch_row($result)) // Data
{ echo '<tr><td>'.implode($row,'</td><td>')."</td></tr>\n"; }
echo "</table>\n"
?>
clinnenb at hotmail dot com
05-Aug-2005 11:19
The following will create a PHP array, $array, containing the MySQL query results with array indexes of the same name as field names returned by the MySQL query.
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
$i=0;
foreach ($line as $col_value) {
$field=mysql_field_name($result,$i);
$array[$field] = $col_value;
$i++;
}
}
jimharris at blueyonder dot co dot uk
20-Dec-2004 10:28
The code in the last comment has an obvious mistake in the for loop expression. The correct expression in the for-loop is $x<$y rather than $x<=$y...
$result = mysql_query($sql,$conn) or die(mysql_error());
$rowcount=mysql_num_rows($result);
$y=mysql_num_fields($result);
for ($x=0; $x<$y; $x++) {
echo = mysql_field_name($result, $x).'<br>';
}
colin dot truran at shiftf7 dot com
17-Dec-2004 08:44
T simply itterate through all the field names on a result set try using this.
$result = mysql_query($sql,$conn) or die(mysql_error());
$rowcount=mysql_num_rows($result);
$y=mysql_num_fields($result);
for ($x=0; $x<=$y; $x++) {
echo = mysql_field_name($result, $x).'<br>';
}
This is useful if you have a result set that joins several tables dynamicaly and you are never sure what all the fields will be when you come to display them.
I suggest you place this within a loop through your result rows and include a field flag check around the echo to only show certain data types like this.
$y=mysql_num_fields($result);
while ($row=mysql_fetch_array($result)) {
for ($x=0; $x<=$y; $x++) {
$fieldname=mysql_field_name($result,$x);
$fieldtype=mysql_field_type($result, $x);
if ($fieldtype=='string' && $row[$fieldname]!='')
echo $row[$fieldname].' , ';
}
echo '<br>';
}
aaronp123 att yahoo dott comm
21-Feb-2003 10:27
You could probably elaborate on this by sending a full sql query to this function...but I titled it simple_query() because it doesn't really allow for joins. Never the less, if you want to get a quick array full of a single row result set this is painless:
function simple_query($table_name, $key_col, $key_val) {
// open the db
$db_link = my_sql_link();
// query table using key col/val
$db_rs = mysql_query("SELECT * FROM $table_name WHERE $key_col = $key_val", $db_link);
$num_fields = mysql_num_fields($db_rs);
if ($num_fields) {
// first (and only) row
$row = mysql_fetch_assoc($db_rs);
// load up array
for ($i = 0; $i < $num_fields; $i++) {
$simple_q[mysql_field_name($db_rs, $i)] = $row[mysql_field_name($db_rs, $i)];
}
// and return
return $simple_q;
} else {
// no rows
return false;
}
mysql_free_result($db_rs);
}
**Please note that my_sql_link() is just a function I have to open up a my sql connection.**
jason dot chambes at phishie dot net
21-Feb-2003 10:07
<?
/*
By simply calling the searchtable() function
with these variables it will serach the desired
database and procude a table for each field that
there is a match.
*/
function searchtable($host,$user,$pass,$database,$tablename,$userquery)
{
$link = mysql_connect($host, $user, $pass) or die("Could not connect: " . mysql_error());
$db = mysql_select_db($database, $link) or die(mysql_error());
$fields = mysql_list_fields($database, $tablename, $link);
$cols = mysql_num_fields($fields);
for ($i = 1; $i < $cols; $i++) {
$allfields[] = mysql_field_name($fields, $i);
}
foreach ($allfields as $myfield) {
$result = mysql_query("SELECT * FROM $tablename WHERE $myfield like '%$userquery%' ");
if (mysql_num_rows($result) > 0){
echo "<h3>search <i>$database</i> for <i>$userquery</i>, found match(es) in <i>$myfield</i>: </h3>\n";
echo "<table border=1 align=\"center\">\n\t<tr>\n";
for ($i = 1; $i < $cols; $i++) {
echo "\t\t<th";
if ($myfield == mysql_field_name($fields, $i)){
echo " bgcolor=\"orange\"> ";
} else {
echo ">";
}
echo mysql_field_name($fields, $i) . "</th>\n";
}
echo "\t</tr>\n";
$myrow = mysql_fetch_array($result);
do {
echo "\t<tr>\n";
for ($i = 1; $i < $cols; $i++){
echo "\t\t<td> $myrow[$i] </td>\n";
}
echo "\t</tr>\n";
} while ($myrow = mysql_fetch_array($result));
echo "</table>\n";
}
}
}
searchtable($host,$user,$pass,$database,$tablename,$userquery);
?>
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
wade at staffordware dot com
05-Nov-1999 08:06
If a field returned by the result contains NULL then that field name will not be returned (WIN32).
|  |