How to put the last row number of a Mysql table into a variable

Asked

Viewed 513 times

2

I would like to know how to take the last row of a Mysql table, and put this number inside a PHP variable.

I tried so:

require_once "../../models/conex.php";

$sql = "SELECT * FROM tabela ORDER BY id DESC LIMIT 1";
$query = $mysqli->query($sql);
$ultimaLinha = $query;
$mysqli->close();

But the var_dump returns boolean false...

  • 1

    You are closing the connection before catching the result, missed the fetch(). Or your real query has some error, because the idea is to sort by id in descending order and return a line.

  • 1

    Don’t forget to see if there are any mistakes at the consulate $query = $db->query($sql) or die($db->error);

  • @rray, $db in case is the connection link? in my case $mysqli?

  • @rray besides forgetting the fetch, had an error in the same query, was passing the name of the wrong field. After I put the $query = $mysqli->query($sql) or die($mysqli->error);that you suggested, appeared the error Unknown column 'id' in 'order clause' and then bingo.... It went 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']);
}

  • Creates an answer,

3 answers

4

Implement this way and do not forget to recover the error, there may be some exception and you are not recovering.

Try this:

$leitura = "SELECT * FROM php_teste"; <== seu select
$resultado =mysqli_query($leitura) or die (mysqli_error());
if(mysqli_num_rows($resultado) <=0){
    echo 'não tem nada';
} else {
    while($mostra = mysqli_fetch_assoc($query)){
        print_r($mostra);
    }
}
  • 1

    So, it didn’t work... from what I saw here mysqli_query also asks the connection link as a parameter, and the mysql_error asks for the variable of query... and in the while I think it would be while($mostra = mysqli_fetch_assoc($resultado)) right (is $query)... but even putting mysqli_query($conn, $leitura) or die (mysqli_error($leitura));, and changing the while didn’t work, and no mistake appears... Until I think it’s some silly mistake I’m making here... +1 by force... hug.

  • Do the following by opening phpAdmin and running this query directly on it, make sure there is data. It already happened to me and did not see the result and the problem was simple, it had not even result. Do it.... success

4


For the record, the problem was that, in addition to forgetting the 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 @rray’s suggestion, error appeared:

Unknown column 'id' in 'order clause'

So it was just to include the while with the fetch, and correct the field name.

Stayed 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 @Danielomine’s guidance in the comments, I changed the while for if, for two reasons:

  • as only return a result, do not need the while;
  • putting the 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";

    }
    ?>
  • does not need the repeat loop as it will return only one result.

  • @Danielomine trading for a if it worked tbm... it’s more correct that way, for him to do a check (if $query->fetch_assoc() for true...), or passing like that is better: $idcerto = NULL;&#xA; $dados = $query->fetch_assoc();&#xA; $idcerto = (int)(($dados['idcalc']) + 1);&#xA; echo $idcerto; (works tbm) There is no answer?

  • 1

    that! puts an if() because it can also come empty. if ($dados = $query->fetch_assoc()){print_r($dados);}else{echo 'vazio';}

  • Cool, cheer up make a response... thanks! @Danielomine

  • is that it makes no sense to answer. Edit your answer with if(), by removing while() there it is!

  • OK Thanks! Just one more question, I declare the variable directly inside the if, or I declare later do the if, so: $dados = $query->fetch_assoc();&#xA; if ($dados = $query->fetch_assoc()) {&#xA; $idcerto = (int)(($dados['idcalc']) + 1);}? @Danielomine

  • 1

    can be within the if(). if ($dados = $query->fetch_assoc())

Show 2 more comments

1

Place:

$linha = $query->fetch_array(MYSQLI_ASSOC);
$variavel=$linha["NOME DA COLUNA DESEJADA"];

Browser other questions tagged

You are not signed in. Login or sign up in order to post.