You are here: Re: Retrieving fields name from mysql « PHP Programming Language « IT news, forums, messages
Re: Retrieving fields name from mysql

Posted by NC on 04/10/06 19:39

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

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация