473,396 Members | 1,683 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,396 software developers and data experts.

Count problem

I've been trying to work out a problem but I'm having no luck. I have
field in my table for area and I need to count the number of matches
and assign a variable for each one.. this is what I have but it doesn't
work.. any ideas?

$testcount = mysql_query("select area, count(area) as newcount from rea
group by area order by area");
while ($returncount = mysql_fetch_array($testcount)) {
if ($returncount[area] == 'RCC') {
$rccn = $returncount[newcount];
}
if ($returncount[area] == 'RCL') {
$rccl = $returncount[newcount];
}
if ($returncount[area] == 'RCH') {
$rcch = $returncount[newcount];
}
if ($returncount[area] == 'RCW') {
$rccw = $returncount[newcount];
}
if ($returncount[area] == 'RNK') {
$rnkn = $returncount[newcount];
}
if ($returncount[area] == 'RNK') {
$rnen = $returncount[newcount];
}
}

Jul 17 '05 #1
6 1550
On 20 Jun 2005 11:39:15 -0700, "Bryan" <br********@gmail.com> wrote:
I've been trying to work out a problem but I'm having no luck. I have
field in my table for area and I need to count the number of matches
and assign a variable for each one.. this is what I have but it doesn't
work.. any ideas?
*sigh* In what way does it not work?

What did you expect it to do?
What does it do?
How do the two differ?

Saying "it doesn't work" without explaining what it does do is not helpful and
could result in getting replies that are even more irritable than this one ;-)
$testcount = mysql_query("select area, count(area) as newcount from rea
group by area order by area");
You haven't checked whether this query worked or not.

Check the return value. If it is false, then the query has failed. The error
is available from mysql_error().

It will also show a warning message, unless you have PHP configured to not
show or log warnings.

You must develop code with errors enabled (see the display_errors and
error_reporting options), and preferably warnings set to maximum, else you are
coding blindfolded.

One thing that jumps out is whether the table really called 'rea'? Or did you
mean 'area'?

With error checking and errors displayed, PHP would have told you this, and
you'd be certain about what's wrong - instead you're posting to thousands of
people asking them to guess based on insufficient information.
while ($returncount = mysql_fetch_array($testcount)) {
if ($returncount[area] == 'RCC') {
Associative array keys should be quoted else it'll raise a warning. It
wouldn't stop the code working, though (unless you had a constant named
'area').
$rccn = $returncount[newcount];
}
if ($returncount[area] == 'RCL') {
$rccl = $returncount[newcount];
}
if ($returncount[area] == 'RCH') {
$rcch = $returncount[newcount];
}
if ($returncount[area] == 'RCW') {
$rccw = $returncount[newcount];
}
if ($returncount[area] == 'RNK') {
$rnkn = $returncount[newcount];
}
if ($returncount[area] == 'RNK') {
$rnen = $returncount[newcount];
}
}


OK, so how do you know it didn't work?

You need to post an example of the input data and the output data to prove it
didn't work.

--
Andy Hassall / <an**@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
Jul 17 '05 #2
On Mon, 20 Jun 2005 11:39:15 -0700, Bryan wrote:
I've been trying to work out a problem but I'm having no luck. I have
field in my table for area and I need to count the number of matches
and assign a variable for each one.. this is what I have but it doesn't
work.. any ideas?

$testcount = mysql_query("select area, count(area) as newcount from rea
group by area order by area");
while ($returncount = mysql_fetch_array($testcount)) {
if ($returncount[area] == 'RCC') {
$rccn = $returncount[newcount];
}
if ($returncount[area] == 'RCL') {
$rccl = $returncount[newcount];
}
if ($returncount[area] == 'RCH') {
$rcch = $returncount[newcount];
}
if ($returncount[area] == 'RCW') {
$rccw = $returncount[newcount];
}
if ($returncount[area] == 'RNK') {
$rnkn = $returncount[newcount];
}
if ($returncount[area] == 'RNK') {
$rnen = $returncount[newcount];
}
}


Like Andy said, try some basic error checking first

// always initialise variables before you use them
$aAreas = array();

$sql = "select area, count(area) as newcount from rea group by area order
by area";
$results = mysql_query($sql , $conn); if(! $results || mysql_error($conn)
|| mysql_num_rows($results)<1) {
echo "Unable to get results [$sql] : " . mysql_error($conn);
}
else
{
// why use 10 variables when 1 will do?
while($row = mysql_fetch_array($results)) {
$aAreas[$row[area]] = $row[newcount];
}
// $aAreas will now hold your results with the extra
print_r($aAreas) . "<br>\n";
}

Jul 17 '05 #3
Bryan wrote:
I've been trying to work out a problem but I'm having no luck. I have
field in my table for area and I need to count the number of matches
and assign a variable for each one.. this is what I have but it doesn't
work.. any ideas?

$testcount = mysql_query("select area, count(area) as newcount from rea
group by area order by area");


MySql queries involving count(columname) require a GROUP BY clause, for
starters.

WMD
Jul 17 '05 #4
On Mon, 20 Jun 2005 21:06:21 GMT, Wayne Delia <wm*@deliafamily.net> wrote:
Bryan wrote:
I've been trying to work out a problem but I'm having no luck. I have
field in my table for area and I need to count the number of matches
and assign a variable for each one.. this is what I have but it doesn't
work.. any ideas?

$testcount = mysql_query("select area, count(area) as newcount from rea
group by area order by area");
^^^^^^^^^^^^^
MySql queries involving count(columname) require a GROUP BY clause, for
starters.


Like the one he's already got? :-)

--
Andy Hassall / <an**@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
Jul 17 '05 #5
Andy Hassall wrote:
On Mon, 20 Jun 2005 21:06:21 GMT, Wayne Delia <wm*@deliafamily.net> wrote:

Bryan wrote:
I've been trying to work out a problem but I'm having no luck. I have
field in my table for area and I need to count the number of matches
and assign a variable for each one.. this is what I have but it doesn't
work.. any ideas?

$testcount = mysql_query("select area, count(area) as newcount from rea
group by area order by area");


^^^^^^^^^^^^^
MySql queries involving count(columname) require a GROUP BY clause, for
starters.

Like the one he's already got? :-)


Oh, man - I can't believe that. That's about the third similar mistake
I've made today. I should probably go back to bed right now.

WMD
Jul 17 '05 #6
Thanks for replying -- Please excuse my ignorance. I come from the
world of Excel and I'm having a little difficulty grasping the full
concept of PHP/MySQL.

The first variable (RCC) works fine, it returns "2" which it should.
The second variable (RCL) also returns "2" but should return "26".

The rest of the variables return nothing although most of them have
should have values (and some of the areas haven't yet been entered into
the table). I will try some other troubleshooting suggestions from CJ.

Jul 17 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Kong Li | last post by:
Follow up to this thread, the latest Oracle 9i release 2 patchset (9.2.0.5) fixed the handle count leak problem. The problem is in Oracle client component. Thanks. Kong ----- From: Kong...
3
by: rob.guitar.rob | last post by:
Hello, My last few posts have been revolving aroung the same problem, and I still cant solve it and I would be really appreciate if anyone could spot a problem. a section of my XML goes like...
1
by: Irfan | last post by:
Hello, I am having some problem with count function. I have a report in which has grouping by a person wise and sub grouping in invoied status e.g.. the output will be Irfan Records (First...
8
by: Invalidlastname | last post by:
Hi, We are developing an asp.net application, and we dynamically created certain literal controls to represent some read-only text for certain editable controls. However, recently we found an issue...
8
by: novus | last post by:
Hi, In ASP.net 2.0 I make a control which add the same controls dynamically. In the oninit event I add the controls to the controls collection. After that the loadviewstate event fills in the...
68
by: Martin Joergensen | last post by:
Hi, I have some files which has the following content: 0 0 0 0 0 0 0 1 1 1 1 0 0 1 1 1 1 0 0 1 1 1 1 0 0 1 1 1 1 0 0 0 0 0 0 0
4
by: Igor | last post by:
I have one SELECT statement that needs to return one ntext field from one table and count something from other table, problem is that all fileds that are not in count have to be in group by and...
23
by: Gary Wessle | last post by:
Hi I have a vector<charwhich looks like this (a d d d a d s g e d d d d d k) I need to get the biggest count of consecutive 'd'. 5 in this example I am toying with this method but not sure if...
22
by: MP | last post by:
vb6,ado,mdb,win2k i pass the sql string to the .Execute method on the open connection to Table_Name(const) db table fwiw (the connection opened via class wrapper:) msConnString = "Data Source="...
4
by: haresh.amis | last post by:
hello to all, I m using .net 2.0 and i face a problem that is as under Well I have a checkboxlist which i bound in .cs page now I want to count that how many checkboxes ate checked ( In...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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,...

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.