Solved

MySQL fields return count

Posted on 2006-07-05
6
249 Views
Last Modified: 2011-09-20
Is it possible to query how many feilds are being recived from an sql query? and then pull out the feild names??
0
Comment
Question by:tonelm54
6 Comments
 
LVL 5

Expert Comment

by:winglis4
ID: 17047202
I suppose if you are fetching results into an array (by using mysql_fetch_array or mysql_fetch_assoc) you could see how long the array is using the php function 'count'. see manual at http://www.php.net/manual/en/function.count.php
0
 
LVL 5

Expert Comment

by:winglis4
ID: 17047417
Oops, just re-read the 2nd half of your question. I'll get back to you on that part  : )
0
 
LVL 4

Expert Comment

by:TheMaximumWeasel
ID: 17047455
to get eh amount of rows do something like this

$sql = mysql_query("SELECT field1, field2 FROM some_table");
$number_of_rows = mysql_num_rows($sql);

and field names using

mysql_list_fields("DB_NAME", "TABLE_NAME");

that will list all of the fieldnames.

Max
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 5

Expert Comment

by:winglis4
ID: 17047683
mysql_num_rows will give you a count of how many rows the query returned, not how many fields are in each row. Although I've never used mysql_list_fields, it should give you an array of the field names in the table, not in the result set, but it should be the same if your query is like 'SELECT * FROM tablename'. Check the manual to see if it's what you need. http://www.php.net/manual/en/function.mysql-list-fields.php

good luck
0
 
LVL 35

Expert Comment

by:Raynard7
ID: 17047926
You can pull out the field names by running through the results - running a foreach on each field and returning the name.

I prefer this option as you only have to perform the query once.

http://www.experts-exchange.com/Web/Web_Languages/PHP/Q_21113994.html

Lists how this can be done in operation
0
 
LVL 9

Accepted Solution

by:
tolgaong earned 500 total points
ID: 17048390
$result = mysql_query("select * from tablename");
$fields = mysql_num_fields($result);
echo "Fields= $fields";
for ($i=0; $i < $fields; $i++) {
  echo mysql_field_name($result, $i)."<br>";
}
0

Featured Post

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Display Calender only  When events is scheduled in Worpdress? 1 23
Exporting image to excel from php document 10 28
php convert date format 3 21
PHP Syntax Error 4 27
Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question