如何修复错误:已弃用:必需参数

Yas*_*yad 1 php pdo

如何修复函数错误?

已弃用:必需参数 $orderfield 位于 C:\xampp\htdocs\shop\includes\functions\functions.php 第 8 行中的可选参数 $where 之后

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;
}
Run Code Online (Sandbox Code Playgroud)
<?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>';
}
?>
Run Code Online (Sandbox Code Playgroud)

Lev*_*ole 6

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

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;

}
Run Code Online (Sandbox Code Playgroud)