Simon André Forsberg's answer is great for the PHP sections. I would like to briefly address your SQL section. I have a problem with hard-coding auto-commit SQL into a script in another programming dialect, particularly one which is visible to the end user, such as PHP. You can achieve same or better result by using stored procedures in SQL with the following advantages:
Your SQL database structure is not revealed to the end user, hence giving you an extra layer of safety especially against SQL injection. As mentioned in my comment it is not so big of a deal here, but still good to know.
You can call the same procedure(s) from multiple PHP scripts without having to rewrite any SQL script.
If/when you need to modify the SQL portion for any reason, instead of having to change every PHP script to fit, all you have to do is change the procedure in SQL, which takes minutes and is easy to debug if need be.
So, in your context, here is my suggestion. We can take your whole SQL statement and build a procedure from it in your MySQL client (or I guess you could execute it from PHP as a one-time SQL query, but easier in MySQL client). Please note, you only need to do this one time (per procedure).
DROP PROCEDURE IF EXISTS sp_FindChart;
DELIMITER //
CREATE PROCEDURE sp_FindChart(IN p_chart_id INT)
BEGIN
-- your original query
SELECT
chart_id,
CONCAT(first_name,' ',last_name) as full_name,
expiration_date as exp,
NOW() as now,
/* Unless the %m-%d-%Y format is crucial,
consider perhaps using CAST(date_of_birth AS date) etc. instead.
Format YYYY-MM-DD possibly faster to cast than concatenate */
DATE_FORMAT(date_of_birth,'%m-%d-%Y') as date_of_birth,
DATE_FORMAT(expiration_date,'%M %d, %Y') as expiration_date,
DATE_FORMAT(recommendation_date,'%M %d, %Y') as recommendation_date,
notes
FROM chart
-- link it to the parameter from the procedure, this will be what PHP passes to MySQL
WHERE chart_id = p_chart_id;
END//
DELIMITER ;
Then all you have to do in PHP, instead of this:
$sql = "SELECT chart_id, CONCAT(first_name,' ',last_name) as full_name, expiration_date as exp, NOW() as now, DATE_FORMAT(date_of_birth,'%m-%d-%Y') as date_of_birth, DATE_FORMAT(expiration_date,'%M %d, %Y') as expiration_date, DATE_FORMAT(recommendation_date,'%M %d, %Y') as recommendation_date, notes
FROM chart
WHERE chart_id = '" . mysql_real_escape_string($input_id) . "'
Is this:
$sql = "CALL sp_FindChart('" . mysql_real_escape_string($input_id) . "' ")"
I think my PHP syntax is close anyways, I'm sure you get the idea. I would still recommend starting with Simon's suggestions first, this is secondary.
$row['status'] = (int) ($row['exp'] >= $row['now']);
– MrLore yesterday