Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
条件语句PHP mysqli,reduce_Php_Sql_Database_Mysqli_Prepared Statement - Fatal编程技术网

条件语句PHP mysqli,reduce

条件语句PHP mysqli,reduce,php,sql,database,mysqli,prepared-statement,Php,Sql,Database,Mysqli,Prepared Statement,这很有效,但我觉得这不是解决我问题的最好办法。 我想要我的代码做的是检查location=1,并发送所有位置的消息 function getCurrentMessage($location){ $conn = Connection::getConnection(); if($location == 1) { $query = "SELECT first_name, last_name, description, title, message ,font_size

这很有效,但我觉得这不是解决我问题的最好办法。 我想要我的代码做的是检查location=1,并发送所有位置的消息

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;
}
函数getCurrentMessage($location){ $conn=Connection::getConnection(); 如果($location==1){ $query=“选择名字、姓氏、描述、标题、消息、字体大小、生效日期 来自tbl_消息 在tbl_authors.id_author=tbl_messages.id_author上加入tbl_authors 在tbl_消息上加入tbl_位置。id_位置=tbl_位置。id_位置
有效日期只需重构出所有不依赖于条件的重复部分

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;
}
函数getCurrentMessage($location){ $conn=Connection::getConnection(); 如果($location==1){ $query=“选择名字、姓氏、描述、标题、消息、字体大小、生效日期 来自tbl_消息 在tbl_authors.id_author=tbl_messages.id_author上加入tbl_authors 在tbl_消息上加入tbl_位置。id_位置=tbl_位置。id_位置
生效日期我现在意识到这个问题应该在代码评审中发布 , 但是在学习了更多关于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;
$conn=getConnection();
$query=“选择名字、姓氏、描述、标题、消息、字体大小、日期格式(生效日期,%h:%i%p%m-%d-%Y”)
来自tbl_消息
在tbl_authors.id_author=tbl_messages.id_author上加入tbl_authors
在tbl_消息上加入tbl_位置。id_位置=tbl_位置。id_位置
其中tbl_messages.id_location=IF(?=1,tbl_messages.id_location,?)

和生效日期您可以删除函数开头的位置检查。这将有助于只允许使用一个查询。然后有一个where子句,它将从函数中的参数获取位置,如
where location=?
将是最好的询问位置