2017-05-31 42 views
0

它的工作原理,但我不覺得它是我的問題的最佳解決方案。 我想讓我的代碼做的是檢查位置是否爲1,併發送所有位置的消息。有條件的準備語句PHP mysqli,減少

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子句,它將從函數中的參數中獲取位置,例如'WHERE location =?' – Akintunde007

+0

[code review](https://codereview.stackexchange.com/)將是最好的地方 –

回答

0

我現在認識到這個問題應該已經張貼在代碼審查Code review , 但更多地瞭解MySQL後,我正在使用控制流程函數我的回答想出辦法使這裏清理代碼。
如果您有任何其他想法來進一步清理代碼,請告訴我。

$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; 
0

只是重構出所有不依賴於條件的重複部分。

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;"; 

     if (!$stmt = $conn->prepare($query)) { 
      return false; 
     } 

    } 
    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;"; 

     if (!$stmt = $conn->prepare($query)) { 
      return false; 
     } 
     $stmt->bind_param('i', $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; 
} 

你可以走得更遠,但這只是一個例子。請注意,如果語句準備失敗,該函數如何返回false。由於你在一個函數內部,這將停止執行該函數並返回false,因爲如果準備失敗沒有任何意義。如果你想要一些可以被捕獲的東西,你也可以使用異常。