PHP Condition with ID

0

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

I have a table in the 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 several records, such as id 4, 5,6, 7 etc ...

PHP varies a check and all the records showed me the one with the smallest id.

    
asked by anonymous 10.10.2015 / 14:56

1 answer

2

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

SELECT MIN(id) FROM tabela;

For a query using PHP , you would have something like this:

$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/>";
         ...
    }
  }
}

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

References:

SQL MIN ()

MySQLi - PHP.net

    
10.10.2015 / 15:01