473,414 Members | 1,631 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

How to output COUNT in php array?

Hello,

I keep getting the following error when I output the code below: Notice: Undefined property: stdClass::$count

Expand|Select|Wrap|Line Numbers
  1. $sql = "SELECT COUNT(name), name FROM searches GROUP BY name";
  2.   $result = mysql_query( $sql );
  3.   while($obj = mysql_fetch_object( $result ) ) {
  4.   $pop_players = array();
  5.   $pop_players['COUNT(name)'] = $obj->count;
  6.   $pop_players['name'] = $obj->name;
  7.   $pop_players_two[] = $pop_players;
  8.   }
  9.  
How can I output COUNT(name) in an array?

Thanks for your assistance.
Sep 30 '10 #1

✓ answered by johny10151981

give a new name to the count value as below
Expand|Select|Wrap|Line Numbers
  1. SELECT COUNT(name) as cnt, name FROM searches GROUP BY name
  2.  
and try
Expand|Select|Wrap|Line Numbers
  1. $obj->cnt;

4 2314
johny10151981
1,059 1GB
give a new name to the count value as below
Expand|Select|Wrap|Line Numbers
  1. SELECT COUNT(name) as cnt, name FROM searches GROUP BY name
  2.  
and try
Expand|Select|Wrap|Line Numbers
  1. $obj->cnt;
Sep 30 '10 #2
Thanks very much! That works!
Sep 30 '10 #3
I am also trying to sort from HIGH to LOW by COUNT(name) or cnt. I seem to only be able to sort by the name...do you have any more suggestions. Here is my code so far:

Expand|Select|Wrap|Line Numbers
  1. $sql = "SELECT COUNT(name) as cnt, name FROM searches GROUP BY name DESC LIMIT 10";
  2.   $result = mysql_query( $sql );
  3.   while($obj = mysql_fetch_object( $result ) ) {
  4.   $pop_players = array();
  5.   $pop_players['name'] = $obj->name;
  6.   $pop_players['COUNT(name)'] = $obj->cnt;
  7.   $pop_players_two[] = $pop_players;
  8.   krsort($pop_players_two);
  9.   }
  10.  
  11. foreach( $pop_players_two as $pop_players) { 
  12. [ $pop_players['name']; ] => $pop_players['COUNT(name)'];<br/>
  13. }
  14. ?>
  15.  
Sep 30 '10 #4
This has been resolved by using ORDER BY.

Expand|Select|Wrap|Line Numbers
  1.  $sql = "SELECT COUNT(name) as cnt, name FROM searches GROUP BY name ORDER BY COUNT(name) DESC LIMIT 10";
  2.  
Sep 30 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Goh, Yong Kwang | last post by:
I'm trying to create a function that given a string, tokenize it and put into a dynamically-sized array of char* which is in turn also dynamically allocated based on the string token length. I...
5
by: Tom Lam lemontea | last post by:
Hi all, This is my very first post here, I've seriously tried some programming on C, and shown below is my very first program(So you can expect it to be very messy) that I wrote after I've learned...
41
by: puzzlecracker | last post by:
Given an array of size n and populated with consecutive integers from 1 to n i.e. in random order. Two integers are removed, meaning zero is placed in their places. Give O (n) efficient algorithm...
0
by: JM | last post by:
How can I store following query output to multi dimensional array or something better? I will be using the output for print queue information reference later in program. ObjectQuery...
0
by: Not Available | last post by:
Does anyone have an example?
6
by: Pavils Jurjans | last post by:
Hello, I think this is classical problem, but I can't figure out how to do it in most right way (I know ineffective way though ;) So, I have this method that does some database work and has to...
7
by: theballz | last post by:
Hi, I am learning c programming and come across a problem i cant seem to solve. I have a file which i wish to parse and put certain lines (which do not contain a hash character) into an array...
2
by: FrEaKmAn | last post by:
Hello What I want to do is to output the data in table in this order. Array ( => Array ( => 1 => English
13
by: pereges | last post by:
I've an array : {100,20, -45 -345, -2 120, 64, 99, 20, 15, 0, 1, 25} I want to split it into two different arrays such that every number <= 50 goes into left array and every number 50 goes...
3
by: Wixxel | last post by:
Let my apologize upfront, i'm very new to C#. I have an array i want to iterate and output it to a textbox. Is a textbox the ideal control for the output of my array? If yes, any suggestion would be...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.