How to fix error: Deprecated: Required parameter

How to fix error: Deprecated: Required parameter

如何修复功能错误?

Deprecated: Required parameter $orderfield follows optional parameter $where in C:\xampp\htdocs\shop\includes\functions\functions.php on line 8

function getAllFrom($field, $table, $where = NULL, $and = NULL, $orderfield, $ordering = "DESC") {
    global $con;
    $getAll = $con->prepare("SELECT $field FROM $table $where $and ORDER BY $orderfield $ordering");
    $getAll->execute();
    $all = $getAll->fetchAll();
    return $all;
}
<?php
$allItems = getAllFrom('*', 'items', 'where Approve = 1', '', 'Item_ID');
foreach ($allItems as $item) {
    echo '<div class="col-sm-6 col-md-3">';
    echo '<div class="thumbnail item-box">';
    echo '<span class="price-tag">$' . $item['Price'] . '</span>';
    echo '<img class="img-responsive" src="img.png" alt="" />';
    echo '<div class="caption">';
    echo '<h3><a href="items.php?itemid='. $item['Item_ID'] .'">' . $item['Name'] .'</a></h3>';
    echo '<p>' . $item['Description'] . '</p>';
    echo '<div class="date">' . $item['Add_Date'] . '</div>';
    echo '</div>';
    echo '</div>';
    echo '</div>';
}
?>

您的函数在可选参数之后有必需参数...

getAllFrom($field, $table, $where = NULL, $and = NULL, $orderfield, $ordering = "DESC")

$where $and 都是可选的(因为它们有默认值)。 $orderfield 是必需的,因为它没有默认值。

所以顺序应该是:

getAllFrom($field, $table, $orderfield, $where = NULL, $and = NULL, $ordering = "DESC")

订单 $where$and 也需要...

getAllFrom($field, $table, $where, $and, $orderfield, $ordering = "DESC")

如果您需要一个“不间断”的解决方案,您可以这样做...

function getAllFrom($field, $table, $where = NULL, $and = NULL, $orderfield = null, $ordering = "DESC") {

    if ( $orderfield === null ) {
        throw new InvalidArgumentException( 'Parameter 5 of getAllFrom function is required.' );
    }

    global $con;

    $getAll = $con->prepare("SELECT $field FROM $table $where $and ORDER BY $orderfield $ordering");

    $getAll->execute();

    $all = $getAll->fetchAll();

    return $all;

}