Get higher and lower id with status = 1

Asked

Viewed 122 times

1

I have a table in the database with several lines,

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

Then I also want to know how I get the lowest.

<?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"];



?>

2 answers

2


Friend only with php is not possible, you have to use mysql. Good, but I believe that’s already used. Do the following : in select put Where status = 1 and add a clause in the select order by id desc , this clause will list lines with id from largest to smallest ( descending ) , do a for, receive the first id and break. Do you understand ? It worked out ? Select =

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

And to catch from the smallest to the greatest:

SELECT * FROM table WHERE status = 1 ORDER BY id ASC
  • Can you give me an answer with the code please?

  • @Gonçalo post his select and how it travels , because I’m by cell phone...

  • From here I get the highest id with status = 1?

  • S, because the order by id desc sorts the id lines in descending, and Where only picks id with status 1

  • I couldn’t get it with your code.

  • I just got.

  • Q ue bom @Gonçalo ...

  • @Augustofurlan adapted his answer, because I edited the question with a question more from the author, all right? I just added the reverse option.

Show 3 more comments

2

You don’t need to assign this to php as it would be an unnecessary job.

To get only the sorted number with the highest id just run sql using the MAX(), in this way:

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

To catch the little one:

SELECT numero_sorteado, MIN(id) FROM apostas WHERE status = 1 
  • Good afternoon, yes that would be but the code is in error.

  • What error is returning @Gonçalo ?

  • @Gonçalo the code of the answer seems appropriate and answer the problem, if it does not work, you should add as it is your code in the question, without it, there is no way to guess the cause of the problem.

  • I added the complete code the error you give me is: "Undefined index: numero_drawn" because I want * and it doesn’t have.

  • @Gonçalo the error occurs because only the id column comes, you have to declare all the columns you want to return in select, something like this: select numero_sorteado, max(id) from apostas where status = 1

  • He didn’t make it very clear on the question, but it seems that id and numero_drawn are different columns, as well as status.

  • Truth @Diegof... Gonçalo, you can make it clear if it still doesn’t work with what Diego suggested?

  • Yes, they’re different columns.

  • @Gonçalo then tests the code the way I suggested it will work.

  • Did not work the code shows only the 1st table id.

  • @Ináciorégis I adapted your answer, because I edited the question with a question more from the author, all right? I just added the reverse option.

Show 6 more comments

Browser other questions tagged

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