Warning: mysqli_fetch_assoc() expects parameter 1

2020-05-10 07:12发布

问题:

Trying to get the last row in the table but throwing error ...

'Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, string given in '

$recents = "SELECT * FROM list ORDER BY id DESC LIMIT 1";
if ($result = mysqli_fetch_assoc($recents)) {
    $mName = $result['name'];
    $mDesc = $result['description'];
    $mCost = $result['cost'];
}

回答1:

You need to pass a result from a query, not the query string.

$sql = "SELECT * FROM list ORDER BY id DESC LIMIT 1";
$recent = mysqli_query($connetion, $sql);


回答2:

You forgot to use the statement to run the query

$results=mysqli_query($conn,$recents)