jeff5656 Posted October 16, 2008 Share Posted October 16, 2008 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) Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted October 16, 2008 Share Posted October 16, 2008 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. Quote Link to comment Share on other sites More sharing options...
ngreenwood6 Posted October 16, 2008 Share Posted October 16, 2008 $fu is not going to echo anything because it looks like to me that you never set it an original value. please correct me if I am wrong. Also I do not think that you can simply do this: $old_date = date ("Y-m-d")-4; You will probably have to change that into another statement. Quote Link to comment Share on other sites More sharing options...
Guest Posted October 16, 2008 Share Posted October 16, 2008 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. Quote Link to comment Share on other sites More sharing options...
jeff5656 Posted October 16, 2008 Author Share Posted October 16, 2008 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! Quote Link to comment Share on other sites More sharing options...
Guest Posted October 16, 2008 Share Posted October 16, 2008 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"; Quote Link to comment Share on other sites More sharing options...
jeff5656 Posted October 16, 2008 Author Share Posted October 16, 2008 Thank you 8ball!! I JUST realized that before coming back here. I was not selecting all the variables from the database. When I changed it to select *, it now works! Quote Link to comment Share on other sites More sharing options...
Guest Posted October 16, 2008 Share Posted October 16, 2008 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! Quote Link to comment Share on other sites More sharing options...
Barand Posted October 16, 2008 Share Posted October 16, 2008 you don't select a column "rcf1" nor do you allocate it as an alias, so there is no $row['rcf1'] and hence $fu is never set. Also $curr_date = date ("Y-m-d"); $old_date = date ("Y-m-d", strtotime('-4 years')); Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted October 16, 2008 Share Posted October 16, 2008 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. Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted October 17, 2008 Share Posted October 17, 2008 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.