Jump to content

Multiple Condition MySql Query

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
5 replies to this topic

#1
Furry Pancake

Furry Pancake

    Learning Programmer

  • Members
  • PipPipPip
  • 54 posts
EOB 10/5 denied the claim needing records. These were mailed with the claim. I will submit this claim on the BCBS NC MRR spreadsheet

Hey Guys,
I know this may sound retarded but I must have hit a wall or something.

I am trying to echo a report that will count 2 types of information. Members that have a specific gender, who have been activated, between dates X and Y. Then Members with a specific gender who have NOT been activated, between dates X and Y. So far I have a function that looks like this:


function active_info($date1,$date2) {

	$query = mysql_query("SELECT * FROM users WHERE gender='male' AND date_registered BETWEEN '$date1' AND '$date2'");

	$count_query = mysql_num_rows($query);

	echo "There are currently $count_query user(s) who are male and registered between dates $date1 and $date2";

}


BUT,
I need to change it to look something like this:


function active_info($date1,$date2) {

	$query = mysql_query("SELECT * FROM users WHERE gender='male' AND activated='yes' AND date_registered BETWEEN '$date1' AND '$date2'");

	$count_query = mysql_num_rows($query);

	echo "There are currently $count_query user(s) who are male and registered between dates $date1 and $date2";

}


But, as we all know, that won't work.

Will you please help me out. Lol

Thank you!
Durr dah dur.

#2
DEViANT

DEViANT

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 358 posts
Maybe I'm dwarf but, that should work? I can't see anything wrong with it.

Also, you shouldn't echo in your functions. Rather return string values.

:D You should rep+ me so that I can win :D

My Blog | Ask me!
Error : Satan did it

#3
Furry Pancake

Furry Pancake

    Learning Programmer

  • Members
  • PipPipPip
  • 54 posts
Sorry, the echo inside of the function was being used as an example :) But for some reason, it's not working for me at all..

It won't throw any types of errors, but then again it won't echo any data. Strange :crying:
Durr dah dur.

#4
DEViANT

DEViANT

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 358 posts

function active_info($date1,$date2) {

    $query = mysql_query("SELECT * FROM users WHERE gender='male' AND activated='yes' AND date_registered BETWEEN '$date1' AND '$date2'") or die(mysql_error());

    $count_query = mysql_num_rows($query);

    $text = "There are currently $count_query user(s) who are male and registered between dates $date1 and $date2";

return $text;

}  


Try running that? See if it gives you a mysql error

Also, are the dates being passed into the function in the same structure as the date in the mysql table?

If I remember correctly mysql uses The 'Y/m/d' structure. The time being passed to it to check should be in the same structure

:D You should rep+ me so that I can win :D

My Blog | Ask me!
Error : Satan did it

#5
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
MySQL normally accept a lot of date formats, but it's genuine standard is the ISO-Standard, yyyy-mm-dd
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#6
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
There is nothing wrong with your query syntax, there are no rows that match your constraints. DEViANT is right in that you should verify the data you are constraining and the data that is actually in the database.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.