[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

PHP MySQL multiple mysql_num_rows in one query

Posted on 2009-04-07
8
Medium Priority
?
411 Views
Last Modified: 2013-12-12
I have a MySQL table that I need to generate reports from.  One column is called reason_code.  There are over 100 possible reason codes in the table.  I need to generate a report that tells me how many records  are in the table for each reason_code value.  

I know how to find the number of records for one reason code, but don't know how to do this for over 100 reason codes, without running 100 querries.  Please help me simplify my code below.  The code below shows a separate query for each reason code.  What I want is one querry that assigns the number of records to a variable for all reason codes in the table.  The following code works, but it is a killer script if I have to write a separate query for each reason code type.
$query = sprintf("SELECT amount, card_type FROM pdf_made_cb WHERE mid = '$mid' AND receive_date >= '$date1' AND receive_date <= '$date2' AND amount >= $amt1 AND amount <= $amt2 AND reason_code = 'Code 1' ");
$rs_v_c = mysql_query($query, $connect_crm) or die(mysql_error());
$rca1 = mysql_num_rows($rs_v_c);
 
$query = sprintf("SELECT amount FROM pdf_made_cb WHERE mid = '$mid' AND receive_date >= '$date1' AND receive_date <= '$date2' AND amount >= $amt1 AND amount <= $amt2 AND reason_code = 'Code 2' ");
$rs_v_c = mysql_query($query, $connect_crm) or die(mysql_error());
$rca2 = mysql_num_rows($rs_v_c);
 
$query_rs_v_c = sprintf("SELECT amount FROM pdf_made_cb WHERE  mid = '$mid' AND receive_date >= '$date1' AND receive_date <= '$date2' AND amount >= $amt1 AND amount <= $amt2 AND reason_code = 'Code 3' ");
$rs_v_c = mysql_query($query, $connect_crm) or die(mysql_error());
$rca3 = mysql_num_rows($rs_v_c);
 
$query = sprintf("SELECT amount FROM pdf_made_cb WHERE mid = '$mid' AND receive_date >= '$date1' AND receive_date <= '$date2' AND amount >= $amt1 AND amount <= $amt2 AND reason_code = 'Code 4' ");
$rs_v_c = mysql_query($query, $connect_crm) or die(mysql_error());
$rca4 = mysql_num_rows($rs_v_c);

Open in new window

0
Comment
Question by:fastfind1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 200 total points
ID: 24084839
something this would do:
$query = sprintf("SELECT reason_code, count(*) rows, sum(amount) sum_amount FROM pdf_made_cb WHERE mid = '$mid' AND receive_date >= '$date1' AND receive_date <= '$date2' AND amount >= $amt1 AND amount <= $amt2 group by reason_code ");
$rs_v_c = mysql_query($query, $connect_crm) or die(mysql_error());
$rca1 = mysql_num_rows($rs_v_c);

Open in new window

0
 
LVL 4

Expert Comment

by:bleach77
ID: 24084946
try this.
$query_rs_v_c = sprintf("SELECT reason_code, count(amount) AS amount FROM pdf_made_cb WHERE  mid = '$mid' AND receive_date >= '$date1' AND receive_date <= '$date2' AND amount >= $amt1 AND amount <= $amt2 GROUP BY reason_code");
$rs_v_c = mysql_query($query, $connect_crm) or die(mysql_error());
 
$code = array();
 
while ($row = mysql_fetch_assoc($rs_v_c)) {
   $code["$row['reason_code']"] = $row['amount'];
}  

Open in new window

0
 

Author Comment

by:fastfind1
ID: 24084949
Thank you for your help.  I need to assign a variable to each count of records for each reason code.  So if reason code 1 had 4 records, I need to assign $rca = 4.  If reason code 2 had 9 records, I need to assign $rca = 2.  How do I do this based on your code?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:fastfind1
ID: 24084967
i meant, if reason code 2 had 9 records i need to assign $rcb = 9.  Sorry for the confusion.
0
 

Author Comment

by:fastfind1
ID: 24084978
In the end, I need something like this:

$rca = the number of records with reason code 1;
$rcb = the number of records with reason code 2;
$rcc = the number of records with reason code 3;
$rcd = the number of records with reason code 4;
0
 
LVL 4

Expert Comment

by:bleach77
ID: 24085039
The old code would be like
$code['Code 1'] = the number of records with reason code 1;
$code['Code 2'] = the number of records with reason code 2;
$code['Code 3'] = the number of records with reason code 3;

This code will produce like your first post
$rca1 =  the number of records with reason code 1;
$rca2 =  the number of records with reason code 2;
$rca3 =  the number of records with reason code 3;
$query_rs_v_c = sprintf("SELECT reason_code, count(amount) AS amount FROM pdf_made_cb WHERE  mid = '$mid' AND receive_date >= '$date1' AND receive_date <= '$date2' AND amount >= $amt1 AND amount <= $amt2 GROUP BY reason_code ORDER BY reason_code");
$rs_v_c = mysql_query($query, $connect_crm) or die(mysql_error());
 
$count = 0;
 
while ($row = mysql_fetch_assoc($rs_v_c)) {
  $count++;
  ${"rca".$count} = $row['amount'];
}  

Open in new window

0
 
LVL 4

Accepted Solution

by:
bleach77 earned 1800 total points
ID: 24085054
This should work like your new post
$rca = the number of records with reason code 1;
$rcb = the number of records with reason code 2;
$rcc = the number of records with reason code 3;
$rcd = the number of records with reason code 4;
$query_rs_v_c = sprintf("SELECT reason_code, count(amount) AS amount FROM pdf_made_cb WHERE  mid = '$mid' AND receive_date >= '$date1' AND receive_date <= '$date2' AND amount >= $amt1 AND amount <= $amt2 GROUP BY reason_code ORDER BY reason_code");
$rs_v_c = mysql_query($query, $connect_crm) or die(mysql_error());
 
$count = 'a';
 
while ($row = mysql_fetch_assoc($rs_v_c)) {
  ${"rc".$count} = $row['amount'];
  $count++;
}  

Open in new window

0
 
LVL 4

Expert Comment

by:bleach77
ID: 24085120
Both way you need to either declare the variables first.
$rca1 = $rca2 = $rcb = "";

OR

when you want to use it, you have to check using isset, otherwise an error will occur.

if(isset($rca1)) echo $rca1;
if(isset($rca2)) echo $rca2;
if(isset($rcb)) echo $rcb;
0

Featured Post

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read on. . .
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question