We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

Trying to Count categories and return value using a function

befidled
befidled asked
on
Medium Priority
282 Views
Last Modified: 2012-05-11
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?
Comment
Watch Question

SharathData Engineer
CERTIFIED EXPERT
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview

Author

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?

Author

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?

Author

Commented:
Got it, I just put $subcategory in quotes, and escaped them.

thanks!

Author

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
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.