それは動作しますが、私はそれが私の問題のための最良の解決策であるとは思わない。 私のコードで行うべきことは、location = 1であるかどうかをチェックして、すべての場所のメッセージを送信することです。条件付きプリペイントステートメントPHP mysqli、reduce
function getCurrentMessage($location){
$conn = Connection::getConnection();
if($location == 1) {
$query = "SELECT first_name, last_name, description, title, message ,font_size , effective_date
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
AND effective_date <= CURDATE()
ORDER BY effective_date desc
LIMIT 2;";
$result = array();
if ($stmt = $conn->prepare($query)) {
$stmt->bind_result($first_name, $last_name, $location, $title, $message, $size, $date);
$stmt->execute();
while ($stmt->fetch()) {
$message = new Message($first_name, $last_name, $location, $title, $message, $size, $date);
array_push($result, $message);
}
}
}
else{
$query = "SELECT first_name, last_name, description, title, message ,font_size , effective_date
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 = ?
AND effective_date <= CURDATE()
ORDER BY effective_date desc
LIMIT 2;";
$result = array();
if ($stmt = $conn->prepare($query)) {
$stmt->bind_result($first_name, $last_name, $location, $title, $message, $size, $date);
$stmt->bind_param('i', $location);
$stmt->execute();
while ($stmt->fetch()) {
$m = new Message($first_name, $last_name, $location, $title, $message, $size, $date);
array_push($result, $m);
}
}
}
return $result;
}
多分、私はいくつかのロジックをSQL文に入れることができます。 洞察力があれば、助けてください。
機能の始めにチェックを外すことができます。これは、1つのクエリだけを使用できるようにすることによって役立ちます。 WHERE location =?のような関数内のパラメータから場所を取るwhere節があります。 – Akintunde007
[コードレビュー](https://codereview.stackexchange.com/)は、質問するのに最適な場所です –