Edmhar
Edmhar

Reputation: 650

Calling Count column php mysql

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)

Upvotes: 3

Views: 62

Answers (1)

Funk Forty Niner
Funk Forty Niner

Reputation: 74217

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."

Upvotes: 3

Related Questions