Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 271
  • Last Modified:

Trying to Count categories and return value using a function

I'm querying my database to find the total number of entries that match a certain condition. I've set up a function that should be returning this number however it isn't returning anything.

function getCompanyCount($subcategory) {
	
	$countQuery = "SELECT COUNT(*) AS myCount FROM (SELECT wp_rg_lead_detail.id, 
						wp_rg_lead_detail.lead_id, 
						wp_rg_lead_detail.form_id, 
						wp_rg_lead_detail.field_number, 
						wp_rg_lead_detail.value
					FROM wp_rg_lead_detail
					WHERE wp_rg_lead_detail.form_id  = 3 AND wp_rg_lead_detail.value = '$subcategory') AS recordcount";
	
	$results = mysql_query($countQuery);					
	
	return $results->myCount;
	
}

Open in new window


What am I missing?
0
befidled
Asked:
befidled
  • 4
2 Solutions
 
SharathData EngineerCommented:
WHy don't you try like this?
function getCompanyCount($subcategory) {
	
	$countQuery = "SELECT COUNT(*) 
	                 FROM wp_rg_lead_detail
	                WHERE wp_rg_lead_detail.form_id  = 3 AND wp_rg_lead_detail.value = '$subcategory'";
	
	$results = mysql_query($countQuery);					
	
	return $results->myCount;
	
}

Open in new window

0
 
minjosefaCommented:
$results = mysql_query($countQuery);
$row = mysql_fetch_assoc($result);
return $row['myCount'];

Open in new window

0
 
befidledAuthor Commented:
Ok, those suggestions seem  to be working, but I am getting a sporadic MySQL error;

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/ventura1/public_html/wp-content/themes/672_wp/directory-list-test-2.php on line 42

What might cause this?
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
befidledAuthor Commented:
ah, it's only returning that error when the $subcategory value has an apostrophe in it.

If I have a subcategory that has an apostophe, and it also resides in the database with the apostrophe, what is the best way to run the comparison without breaking the script?
0
 
befidledAuthor Commented:
Got it, I just put $subcategory in quotes, and escaped them.

thanks!
0
 
befidledAuthor Commented:
Thanks guys.

Shareth, your code was more efficient than what I had so I gave you some points as well. It didn't really address my problem but thanks.

brian
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now