Just to note, the problem was that in addition to forgetting fetch
(which was pointed out by @rray), also had an error in the query, as it was passing the wrong field name.
After I put the
$query = $mysqli->query($sql) or die($mysqli->error);
At the suggestion of @rray, the error appeared:
Unknown column 'id' in 'order clause'
Then just include while
with fetch
, and correct the field name.
It looks like this:
require_once "../../models/conex.php";
$sql = "SELECT * FROM tabela ORDER BY idcerto DESC LIMIT 1";
$query = $mysqli->query($sql) or die($mysqli->error);
while ($dados = $query->fetch_assoc()) {
print_r($dados['idcerto']);
}
UPDATE
Following the guidance given by @DanielOmine in comments, I changed while
to if
, for two reasons:
- As it will only return a result, it does not need
while
;
- putting
if
it checks if the result does not return empty;
The standard code:
require_once "../../models/conex.php";
$sql = "SELECT * FROM tabela ORDER BY idcerto DESC LIMIT 1";
$query = $mysqli->query($sql) or die($mysqli->error);
if ($dados = $query->fetch_assoc()) {
$idcerto = (int)(($dados['idcalc']) + 1);
echo $idcerto;
}
else{
echo "erro na obtenção da id";
}
?>