2017-05-31 45 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,因为如果准备失败没有任何意义。如果你想要一些可以被捕获的东西,你也可以使用异常。