Solved

MySQL fields return count

Posted on 2006-07-05
6
246 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
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 count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

708 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now