Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP/MySQL - How do you determin the field names from a given query result?

Tags:

php

mysql

Given a result set, how can I determin the actual names of the fields specified in the query (NOT their aliases).

$query = "SELECT first AS First_Name, last AS Last_Name FROM people";
$dbResult = mysql_query($query);

$fieldCount = mysql_num_fields($dbResult);
for ($i=0; $i<$fieldCount; $i++) {          
    // Set some values
    $fieldName = mysql_field_name($dbResult, $i);
}

This example returns field names, but in this example it returns the alias "First_Name" instead of the actual field name "first".

Is it possible to get the actual field name from such a query. Particularly if I am writing a function and have no idea what query will be thrown at it.

like image 573
user34418 Avatar asked Nov 30 '22 07:11

user34418


1 Answers

If you are using MySQLi:

http://www.php.net/manual/en/mysqli-result.fetch-field.php

The field object has a "orgname" property.

The "classic" MySQL equivalent function doesn't report back the original column names.

like image 152
bobwienholt Avatar answered Dec 06 '22 09:12

bobwienholt