Solved

MySQL fields return count

Posted on 2006-07-05
6
250 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

860 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