Solved

MySQL fields return count

Posted on 2006-07-05
6
252 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

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

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.  …
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
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 …

719 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