473,395 Members | 1,948 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Retrieving fields name from mysql

I was wondering if there is a way to collect the names of the fields
from a specific table. I think the soluction is to be researched in the
sql code but maybe someone knows of a way to o so directly from php.

Example.
Table: Categories
Field1: type1
Field2: type2
Field3: type3
Field4: type4
Field5: type5
What I want is to be able to have an array (or an object) that cointans
the name of the fields:

array[0]=type1
array[1]=type2
array[2]=type3
array[3]=type4
array[4]=type5

Any suggestions?

Apr 10 '06 #1
6 1799

As far as I'm aware there is no way to do it accept use the "describe"
function of mysql (or sql or whatever).

You can call this using the php mysql query command. I use mysqli in
PHP5, it becomes even easier then.

I haven't tested this but try the following:

// Initialise the type array to empty.
$TypeArray = array();

// Perform the mysql "describe" of the table Categories.
$QueryResult = $this->m_mysqli->query("describe Categories");

// Error checking for above function here, yada yada yada ;o)

// Convert the first result into an object.
$ResultObject = $QueryResult->fetch_object();

// If there was a result then...
while ($ResultObject !== NULL)
{
// Get the type...
$Type = $ResultObject->Type;

// And push it onto the type array.
array_push($TypeArray, $Type);

// Get the next result.
$ResultObject = $QueryResult->fetch_object();
}

Apr 10 '06 #2

Mike Youell wrote:
As far as I'm aware there is no way to do it accept use the "describe"
function of mysql (or sql or whatever).


You may want to look at the function mysql_fetch_field(), which
according to the manaual does "Returns an object containing field
information. This function can be used to obtain information about
fields in the provided query result."

Ken

Apr 10 '06 #3
NC
Duderino82 wrote:

I was wondering if there is a way to collect the names of the fields
from a specific table. I think the soluction is to be researched in the
sql code but maybe someone knows of a way to o so directly from php.

Example.
Table: Categories
Field1: type1
Field2: type2
Field3: type3
Field4: type4
Field5: type5
What I want is to be able to have an array (or an object) that cointans
the name of the fields:

array[0]=type1
array[1]=type2
array[2]=type3
array[3]=type4
array[4]=type5

Any suggestions?


There are several possible ways of doing it. One is to execute a SHOW
CREATE TABLE query and parse the result. Another is to run a SHOW
FIELDS FROM Categories query:

$fields = array();
$result = mysql_query('SHOW FIELDS FROM Categories');
while ($record = mysql_fetch_array($result, MYSQL_NUM)) {
$fields[] = $record[0];
}

Now the $fields array should contain names of all fields in the
table...

Yet another way is to retrieve a random record and look at its
structure. Something like this:

$fields = array();
$result = mysql_query('SELECT * FROM Categoties LIMIT 1');
$record = mysql_fetch_array($result, MYSQL_ASSOC);
foreach ($record as $field=>$value) {
$fields[] = $field;
}

Now, again, the $fields array should contain names of all fields in the
table...

Cheers,
NC

Apr 10 '06 #4
Mike Youell wrote:
As far as I'm aware there is no way to do it accept use the "describe"
function of mysql (or sql or whatever).


http://uk2.php.net/function.mysql-list-fields explains all.

You could also, I expect, use "select * from table limit 0,1"
and then array_keys() on the resulting row.

Apr 10 '06 #5
Following on from Mike Youell's message. . .

MySql:-
Look up SHOW COLUMNS for what the database looks like.

If you want to see the field names for a live results set you can use
the flag that sets the associated name in the mysql_[err] - I'm not at
mt PHP system] fetch function and extract the keys from the array result
set.


As far as I'm aware there is no way to do it accept use the "describe"
function of mysql (or sql or whatever).

You can call this using the php mysql query command. I use mysqli in
PHP5, it becomes even easier then.

I haven't tested this but try the following:

// Initialise the type array to empty.
$TypeArray = array();

// Perform the mysql "describe" of the table Categories.
$QueryResult = $this->m_mysqli->query("describe Categories");

// Error checking for above function here, yada yada yada ;o)

// Convert the first result into an object.
$ResultObject = $QueryResult->fetch_object();

// If there was a result then...
while ($ResultObject !== NULL)
{
// Get the type...
$Type = $ResultObject->Type;

// And push it onto the type array.
array_push($TypeArray, $Type);

// Get the next result.
$ResultObject = $QueryResult->fetch_object();
}


--
PETER FOX Not the same since the cardboard box company folded
pe******@eminent.demon.co.uk.not.this.bit.no.html
2 Tees Close, Witham, Essex.
Gravity beer in Essex <http://www.eminent.demon.co.uk>
Apr 10 '06 #6
Thankx a lot! You info was very usefull!

Apr 12 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Josep | last post by:
Hi, I'd like to poll a database and get the table contents, as well as the field names from that table. I've been to php.net but this time I cannot find something helpful. I can get the data,...
0
by: Andy | last post by:
Hi All. I'm working for a company that has set out a guideline for retrieving data from a database. Nobody can explain to me the reason for the following. When retrieving a set of records...
13
by: no.mail.pls | last post by:
Hiya, How do i retreive fields with similar values from 2 tables? I tried to use (1) "SELECT * FROM $table1 as o , $table2 as p WHERE o.name like '%p.name%'"; but it retrieves nothing at...
5
by: himilecyclist | last post by:
I have a query screen where the user has an option to search by name fields in the database. There are first, middle and last name fields and the results returned should be sorted last, first,...
4
by: monomaniac21 | last post by:
hi! is it possible to do the aforementioned query - selecting only distinct in 1 col but retrieving all other cols at the same time. regards marc
1
by: commodityintelligence | last post by:
Greetings, I am merging a series of different tables into one query to export decision-making information. I have some architecture issues I need to ask for help on. I have no programming...
0
bmallett
by: bmallett | last post by:
First off, i would like to thank everyone for any and all help with this. That being said, I am having a problem retrieving/posting my dynamic form data. I have a form that has multiple options...
9
ADezii
by: ADezii | last post by:
One question which pops up frequently here at TheScripts is: 'How do I retrieve data from a Recordset once I've created it?' One very efficient, and not that often used approach, is the GetRows()...
3
ADezii
by: ADezii | last post by:
Last Tip, we demonstrated the technique for retrieving data from a DAO Recordset, and placing it into a 2-dimensional Array using the GetRows() Method. This week, we will cover the same exact Method...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.