Hi,
Are you just trying to determine the column or field names? Or, trying to change or set those column or field names into their actual values?
Anyway, take a look about the following mysql functions if they can help you:
mysql_field_name
(PHP 3, PHP 4 )
mysql_field_name-- Get the name of the specified field in a result
Description
string mysql_field_name ( resource result, int field_index)
mysql_field_name() returns the name of the specified field index. result must be a valid result identifier and field_index is the numerical offset of the field.
Note: field_index starts at 0.
e.g. The index of the third field would actually be 2, the index of the fourth field would be 3 and so on.
Note: Field names returned by this function are case-sensitive.
Example 1. mysql_field_name() example
<?php
/ The users table consists of three fields:
user_id
username
password.
/
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$db_selected) {
die('Could not set $dbname: ' . mysql_error());
}
$dbname = 'mydb';
$db_selected = mysql_select_db($dbname, $link);
if (!$db_selected) {
___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);
?>
The above example would produce the following output:
user_id password
For downwards compatibility mysql_fieldname() can also be used. This is deprecated, however.
mysql_fetch_field
(PHP 3, PHP 4 )
mysql_fetch_field-- Get column information from a result and return as an object
Description
object mysql_fetch_field ( resource result [, int field_offset])
Returns an object containing field information.
mysql_fetch_field() can be used in order to obtain information about fields in a certain query result. If the field offset isn't specified, the next field that wasn't yet retrieved by mysql_fetch_field() is retrieved.
The properties of the object are:
name - column name
table - name of the table the column belongs to
max_length - maximum length of the column
not_null - 1 if the column cannot be NULL
primary_key - 1 if the column is a primary key
unique_key - 1 if the column is a unique key
multiple_key - 1 if the column is a non-unique key
numeric - 1 if the column is numeric
blob - 1 if the column is a BLOB
type - the type of the column
unsigned - 1 if the column is unsigned
zerofill - 1 if the column is zero-filled
Note: Field names returned by this function are case-sensitive.
Example 1. mysql_fetch_field() example
<?php
$conn = mysql_connect('localhost:3306', 'user', 'password');
if (!$conn) {
_die('Could not connect: ' . mysql_error());
}
mysql_select_db('database');
$result = mysql_query('select from table');
if (!$result) {
die('Query failed: ' . mysql_error());
}
/ get column metadata */
$i = 0;
while ($i < mysql_num_fields($result)) {
echo "Information for column $i:<br />\n";
$meta = mysql_fetch_field($result, $i);
if (!$meta) {
__echo "No information available<br />\n";
}
echo "<pre>
blob:___$meta->blob
max_length:$meta->max_length
multiple_key: $meta->multiple_key
name:______$meta->name
not_null:$meta->not_null
numeric:_$meta->numeric
primary_key:$meta->primary_key
table:____$meta->table
type:___$meta->type
unique_key:$meta->unique_key
unsigned:__$meta->unsigned
zerofill:$meta->zerofill
</pre>";
__$i++;
}
mysql_free_result($result);
?>