Display higher value and name

Asked

Viewed 500 times

0

I have the following code:

    <?php
    $consulta = mysql_fetch_assoc(mysql_query('SELECT MAX(pontos) FROM usuarios'));
    echo  intval($consulta['MAX(pontos)']);
?>

However, it only displays the highest value, I wanted it to display the name of who owns it too.

inserir a descrição da imagem aqui

  • Have you tried selecting the first record by ordering them down? Something like SELECT ... FROM usuarios ORDER BY pontos DESC LIMIT 1.

  • It is because it would not be on the same partition. It would have its own partition, for example: HIGHER SCORER: .. Hence the information, you know?

  • No. What is the relation of this "partition" with SQL?

  • Partition I referred to HTML "div", sorry for irreverence...

  • Have you tried: SELECT login, MAX(pontos) FROM usuarios

  • Sam, I’m pretty layabout in PHP myself. How would you do that, please?

  • 1

    What if there is more than one user with the same maximum score? Should all of them be returned? If not, what should be the criterion to define which will be returned?

  • Next, with version 8 I tested so SELECT nome, MAX(pontos) as maior FROM usuarios group by id limit 1 and it worked fine! https://www.db-fiddle.com/f/ch7WYBguAY3jo9QMZnpLpL/0

  • @Leocaracciolo Deu certinho by coincidence. Change the value of Beltrano to 198 and rotate, will return the Fulano again.

  • No, I don’t know why the cancellation.

  • is that you can only mark 1 response. If you mark the other one, the one that was marked earlier is unchecked.

Show 6 more comments

2 answers

5


Using mysqli:

$conn = new mysqli($servername, $username, $password, $base);
$consulta = mysqli_fetch_assoc(mysqli_query($conn, 'SELECT login, MAX(pontos) as maior FROM usuarios'));
echo intval($consulta['maior']); // imprime "125"
echo $consulta['login']; // imprime "teste2"

In SELECT you tell the columns you want to return separated by a comma, and in MAX(pontos) you create an alias (in case put maior).

Editing

A problem well noted by the friend Anderson Carlos Woss for versions starting with 5.7 of Mysql and which is addressed in this answer and in this.

If you are using a version 5.7 or higher, use the query:

SELECT login, pontos FROM usuarios
WHERE pontos = (SELECT MAX(pontos) FROM usuarios)

Testing on the Dbfiddle

  • 1

    In which version of Mysql did you test? In versions 5.7 and 8.0 that I tested were error due to lack of group by.

  • Dude, I tested it on a Locaweb hosting... I’m going to get here which version....

  • The version is 5.6.

  • If you wish to test: https://www.db-fiddle.com/f/mwpuGfsMfkhCjSAZLqbYVq/0

  • Solution I found: https://www.db-fiddle.com/f/9HzHyDPnX1psNV6zcX6j3M/0

  • 1

    This is the best especially if you consider multiple records with the maximum value.

Show 1 more comment

2

Obsolete Mysql: Do not use mysql functions_*

The Mysql library, which has functions such as mysql_connect, mysql_query and the like, does not allow you to use new Mysql features such as triggers, stored procedures and others. These features are only available with the use of the Mysqli library.

The Mysqli extension has existed since PHP 5.0, which was released on July 13, 2004. Since then, it has been recommended to use Mysqli instead of Mysql.

However, many programmers continued to use (and still use) the Mysql library.

To change this scenario, the PHP team took a somewhat drastic attitude: it removed the Mysql library from PHP 7.

Alternatives to the Mysql extension

  1. Use the Mysqli extension instead of Mysql.
  2. Use PDO extension (PHP Data Object).

Example using PDO

works in any version of Mysql - test here

/******************** conexão *********************************************
$hostname="localhost";  
$username="USUARIO";  
$password="SENHA";  
$db = "Nome_BD";  
$pdo = new PDO("mysql:host=$hostname;dbname=$db", $username, $password);
**************************************************************************/

$sql= "SELECT login, pontos FROM usuarios order by pontos DESC limit 1"; 
$stmt = $pdo->prepare($sql);
$stmt->execute();
$obj = $stmt->fetchObject();
echo $obj->login;
echo $obj->pontos;

Browser other questions tagged

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