Solved

MySQL fields return count

Posted on 2006-07-05
6
247 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The viewer will learn how to dynamically set the form action using jQuery.
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 …

920 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

15 Experts available now in Live!

Get 1:1 Help Now