Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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!