Jump to content

why doesn't this work (if else statement in a query


jeff5656

Recommended Posts

I want to check to see if a record has a date that falls between today and 4 years ago and then if ANY record fulfills this criteria echo "you have follow-ups" otherwise say "no followups.

The following always echos "no followups":

 

<?php

$curr_date = date ("Y-m-d");
$today = strtotime ($curr_date);
$old_date = date ("Y-m-d")-4;

$query = "SELECT id_incr, patient_name, mrn, location, fellow, rcf_date, admission, consult_reason, impression, recs, comments, followup ".
	"FROM active_consults WHERE followup = 'y' ".
	"ORDER BY rcf1 DESC";
$results = mysql_query ($query) or die (mysql_error());
while ($row = mysql_fetch_assoc ($results)) {

if ($row['rcf1'] <= $curr_date && $row['rcf1'] >= $old_date) 
{
$fu==$fu+1;
}
else {
$dd==0;
}
}
if ($fu >=1) {

echo "you have a followup";
}
else {
echo "no followups";
echo $fu;
}
?>

and when I echo $fu, it is blank (not even zero)

Link to comment
Share on other sites

Well, you're using the equality operator (==) rather than the assignment operator (=). To be honest, i'm surprised you dont get an error message.

 

On a related note, why do you think you would get 0 back? $fu is undefined prior to your if statement. 0 and undefined are not the same.

 

And finally, you might want to take a look at the mysql date and time functions. I didn't read your code thoroughly but its more than likely you can do this more efficiently in mysql.

Link to comment
Share on other sites

Yes, you need to replace == with = where you tried to give $fu a value. It means nothing as it is now.

 

For future reference; make sure your display errors and error reporting is on. If you don't have access to your php.ini file, try;

 

error_reporting(E_ALL);

 

Will help to differentiate the source of problems. The next step would be a debugger.

 

EDIT:

To build on gingerrobot's reply: you can do the following for date searches

 

SELECT <fields> FROM <table> WHERE <date> BETWEEN 'yyyy-mm-dd' AND 'yyyy-mm-dd'

 

Assuming <date> is a DateTime field.

Link to comment
Share on other sites

1. I changed == to = and it still echos "no followups"

2. Error reporting is on, but I'm not getting an error, it just goes to the else statement and sees it as untrue.

3. The $old_date does indeed work and returns a value of october 16, 2004.

 

Thanks so far for the help. quick responses!

Link to comment
Share on other sites

Could it be the query itself? Try testing the query in PhpMyAdmin or somewhere closer to your table and see if it returns records. The query itself is syntactically correct, but may logically be incorrect. That's what I suspect at least.

 

EDIT

Oh i see the problem!

 

if ($row['rcf1'] <= $curr_date && $row['rcf1'] >= $old_date) 

 

You're addressing the field "rcf1" according to your query, no such field was selected, although you do order by it. Add rcf1 to the list of fields.

 

$query = "SELECT id_incr, patient_name, mrn, location, fellow, rcf_date, admission, consult_reason, impression, recs, comments, followup, rcf1 ".

      "FROM active_consults WHERE followup = 'y' ".

      "ORDER BY rcf1 DESC";

Link to comment
Share on other sites

Nice, good to hear.

 

On a side note, you may not want to use a wildcard (*) for your SQL statement, especially if you don't intend to use any of that information. You may want to use:

 

SELECT COUNT(patient_name)
FROM active_consults
WHERE rcf1 BETWEEN $curr_date AND $old_date
ORDER BY rcf1 DESC

 

But that's just me being nitpicky XD, anywho, take care!

Link to comment
Share on other sites

Here it is again - one of the purposes of having data in a database is so that you can query for just the items you are interested in, so that you don't have to scan through data to find the items you are interested in.

 

Your current scheme, where you retrieve all the rows WHERE followup = 'y' and then check if there are any rows between two dates will take longer and longer to execute as you add more rows to the table, especially since you are using some slow parsed/tokenized/interpreted php code to do the scanning.

 

If you add the beginning and ending date comparison to your WHERE clause in the query (this has already been shown in one of the posts above), you will just get back the rows (if any) that you are interested in.

Link to comment
Share on other sites

Well, you're using the equality operator (==) rather than the assignment operator (=). To be honest, i'm surprised you dont get an error message.

 

Executing $fu==$fu+1; will simply just evaluate to false. It does, however, not do anything because the return value is not stored nor used anywhere. Syntax-wise it's perfectly legit.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.