php prepared statement and conditional
$conn = getConnection();
$query = "SELECT first_name, last_name, description, title, message ,font_size , DATE_FORMAT(effective_date,'%h:%i %p %m-%d-%Y')
FROM tbl_messages
JOIN tbl_authors ON tbl_authors.id_author = tbl_messages.id_author
JOIN tbl_locations ON tbl_messages.id_location = tbl_locations.id_location
WHERE tbl_messages.id_location = IF(? = 1,tbl_messages.id_location,?)
AND effective_date <= NOW()
ORDER BY effective_date DESC
LIMIT 1
";
if (!$stmt = $conn->prepare($query)) {
return false;
}
$stmt->bind_param('ii', $location,$location);
$result = array();
$stmt->bind_result($first_name, $last_name, $location, $title, $message, $size, $date);
$stmt->execute();
while ($stmt->fetch()) {
$m = new Message($first_name, $last_name, $location, $title, $message, $size, $date);
array_push($result, $m);
}
return $result;