3

I have query that COUNT the data based from user search condition in search.

My question is just simple although I don't know what the solution here:

I want to call the COUNT column, which I know it was just temporary column I have PHP code like this:

$count = mysql_query("SELECT *, COUNT(*) AS SAMPLECOUNT FROM `subscribers` WHERE `country` = 'USA' ");
$row=mysql_fetch_array($count);

so by this code I can echo the columns inside the subscribers by using this:

echo $row['country'];
*echo the count result here*

So maybe the output will be like this:

USA: (the count result)

7
  • 1
    you'll want to look at GROUP BY and Aggregate Functions Commented Nov 12, 2015 at 3:57
  • Ok, What is the output you getting, You stuck up there ? Commented Nov 12, 2015 at 3:58
  • Problem solved I can call by using this echo $row['SAMPLECOUNT];` now I know the alias can be used as column in php Commented Nov 12, 2015 at 4:01
  • 1
    I was just going to say that ^ and wondering why you weren't using that alias. Commented Nov 12, 2015 at 4:03
  • Man make that alias count! Or drop it Commented Nov 12, 2015 at 4:05

1 Answer 1

3

As requested

Since you're using an alias COUNT(*) AS SAMPLECOUNT you pass it along in the $row's array as

echo $row['SAMPLECOUNT'];

in order to show the row count's number.

Here are a few references:

Sidenote: Aliases are case-sensitive on *NIX, but not so on Windows or Mac OSX”.

So echo $row['samplecount']; could fail if on *NIX.


However and quoting this answer https://stackoverflow.com/a/2009011/ on Stack:

"On Unix, table names are case sensitive. On Windows, they are not. Fun, isn't it? Kinda like their respective file systems. Do you think it's a coincidence?

In other words, if you are planning on deploying on a Linux machine, better test your SQL against a Linux-based MySQL too, or be prepared for mysterious "table not found" errors at prod time. VMs are cheap these days.

Field names are case-insensitive regardless."

Sign up to request clarification or add additional context in comments.

3 Comments

You're wrong about nix/win. It's about default behavior only.
I see case sensitive.
@JJ-SAMA It could be an issue on some systems, yes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.