473,672 Members | 2,511 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1813

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("describ e 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($Typ eArray, $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_fie ld(), 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('SH OW FIELDS FROM Categories');
while ($record = mysql_fetch_arr ay($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('SE LECT * FROM Categoties LIMIT 1');
$record = mysql_fetch_arr ay($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("describ e Categories");

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

// Convert the first result into an object.
$ResultObjec t = $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($Typ eArray, $Type);

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


--
PETER FOX Not the same since the cardboard box company folded
pe******@eminen t.demon.co.uk.n ot.this.bit.no. html
2 Tees Close, Witham, Essex.
Gravity beer in Essex <http://www.eminent.dem on.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
11329
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, and retrieve it into an array noproblem. I can get the data+fieldnames and put it into a list.
0
2061
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 from database to a VB.net app, they retrieve the database fields as a record set eg. "select name, suburb from myTable"
13
2194
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 all. (2) "SELECT * FROM $table1 as o , $table2 as p WHERE o.name like p.name";
5
1430
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, middle. Here is the WHERE part of my query: WHERE CONCAT(Last, First, Middle) >= CONCAT('$_POST', '$_POST', '$_POST')"; There is an ORDER BY and LIMIT 200 at the end of this and the results are loaded into a list box.
4
6141
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
1868
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 training. I will explain my structure before asking my questions. I have data from a table (updated monthly by adding the most current month’s data) that is evaluated by certain arithmetic, given an adjective to describe it, and then being exported...
0
3378
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 within options. I have everything being dynamically named from the previously dynamically named element. (I hope this makes sense.) I am not able to retrieve any of the dynamically created values. I can view them on the source page but can't pull them...
9
35466
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() Method of the Recordset Object. This Method varies slightly from DAO to ADO, so for purposes of this discussion, we'll be talking about DAO Recordsets. The ADO approach will be addressed in the following Tip. We'll be using a Query, consisting of 5...
3
43480
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 (GetRows()), but only as it applies to an ADO Recordset. Although there are similarities in the 2 methodologies, the ADO Method offers 2 more Optional Arguments, is a little more complex, and of course, the syntax is different in creating the...
0
8403
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8930
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8605
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8677
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7446
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4227
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4417
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2819
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1816
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.