Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

How do I count the values of a specific column in MySQL + PHP?

So, I'm just trying out to figure out the PHP + MySQL query to count number of times a specific value ("closed") appears within a specific column ("suno") —

I once heard, in programming part of figuring out the solution is writing down what you want to happen in steps, then work out those individual steps in the language, which does help. To a point.

Ideally, I'd like the function to be this...

/* display the number of open museums on that day of the week

Check the day of the week, go through all rows of that day, count the number of "closed" values total them up and minus the figure of the $num variable. Then, echo "there are $open_today museums open today."; */

I'm just tinkering / learning but a bit stuck on this, I'd be super grateful for any help anyone could offer.

Thanks!!

1 Answer

Albert González
Albert González
22,953 Points

You can do a MySQL query like this:

"select count(column_suno) from table_name where suno='open'"

Then store the result in a variable and show it in PHP, it's simple!