Get highest and lowest id with status = 1

1

I have a table in the multi-line database,

I wanted to get the highest id, which has status = 1, how can I do this with php? The id is one column of the table and the status is another.

Then I also want to know how to get the least elevated.

<?php

$link = mysqli_connect("localhost", "root", "vertrigo", "csgodouble");


$contador = 0;



    $verifica = mysqli_query($link, "SELECT MAX(id) FROM apostas WHERE status = 1 ");
$sql = mysqli_fetch_array($verifica);



echo $sql["numero_sorteado"];



?>
    
asked by anonymous 06.03.2016 / 14:46

2 answers

2

Only friend with php is not possible, you have to use mysql. Good, but I think you should be using this already. Do the following: in select put where status = 1 and add a clause in select order by id desc, this clause will list the lines with id from highest to lowest (decreasing), make a for, receive the first id and a break. Understood ? Did it work? Select =

SELECT * FROM table WHERE status = 1 ORDER BY id DESC

And to get from the smallest pro:

SELECT * FROM table WHERE status = 1 ORDER BY id ASC
    
06.03.2016 / 15:17
2

You do not need to assign this to php as it would give unnecessary work.

To get only the number_sorted with the highest id just run sql using MAX() , like this:

SELECT numero_sorteado, MAX(id) FROM apostas WHERE status = 1 

To get the child:

SELECT numero_sorteado, MIN(id) FROM apostas WHERE status = 1 
    
06.03.2016 / 18:11