View Full Version : graphing survey results

06-06-2005, 11:03 PM
Howdy all,
I am looking to put a survey(s) on my site, after a user votes, I would like to be able to pull the current results and then turn it into a graph, my question is - is there a way to count the # of fields in a database? Is this a MySQL function? Or PHP? anybody have any ideas on the best way to do this?

I was thinking to count the # of fields, to determine the number of bars in the graph. Then add up the number of answers to get the upper limit of each bar.


06-07-2005, 09:07 AM
I'm not sure if you can do this directly through mysql, but one way to do it is to get the row with mysql_fetch_assoc which creates an array of that row data then use array_keys to make an array of all the keys in the row and count that, like so:

$row = mysql_fetch_assoc($result);
$num_fields = count(array_keys($row));
echo $num_fields;

SeeIT Solutions
06-07-2005, 09:16 AM
$sql = "SELECT COUNT(*) AS count FROM table;";

That does it in a mysql query (much quicker)

06-07-2005, 12:27 PM
thanks guys, but I figured out a way to do it like this.

$columns = "SHOW COLUMNS FROM polls";

$colres = mysql_query($columns);

$numbars = mysql_num_rows($colres);