PHP condition with ID

Asked

Viewed 29 times

0

I’m a little new to this area, but I’m going to ask my question the same way and try to explain myself as best I can.

I have a table in mysql database, which has id and name and status

I wanted to make a php condition that would check a table in the database and show me the result with the smallest id. That is, a table has multiple records such as id 4, 5,6, 7 etc...

PHP varies a check and from all the logs showed me what had the smallest id.

  • Showing a bit of your code would help you better understand your current stage.

1 answer

2

For this, there is the function MIN() of SQL, that returns the smallest value, see:

SELECT MIN(id) FROM tabela;

For a consultation, using the PHP, you’d have something like that:

$conexao = new mysqli("localhost", "usuario_do_banco", "senha_do_banco", "banco_de_dados");
if($conexao){
  $consulta = "SELECT MIN(id) FROM nome_da_tabela";
  if($resultado = mysqli_query($conexao, $consulta)){
    while($linha = mysqli_fetch_assoc($resultado)){
         echo $linha["id"] . "<br/>";
         echo $linha["outro_campo_nesta_tabela"] . "<br/>";
         ...
    }
  }
}

The consultations SQL with the PHP are something simple, and I will not explain the rest. There are already several questions related to connection to the database, and have been answered.


References:

SQL MIN()

Mysqli - PHP.net

  • In case how would adapt to php?

  • To adapt, you must know before connecting to Mysql.

  • 2

    this answer is enough.. Gonçalo. If you want to know how to use the script, I think the problem is lower. You should start with the PHP be-a-ba

Browser other questions tagged

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