简体   繁体   English

PHP从SQL数据库获取第一行

[英]PHP get first row from SQL database

In a table I am developing, you can see that there is a person with the best known time. 在我正在开发的表格中 ,您可以看到某个人的时间最为人所知。 I would like to show it in my page, and so I used this code: 我想在页面上显示它,因此我使用了以下代码:

  $con=mysqli_connect("localhost","user","pass","database");

  if( $result2 = mysqli_query($con,"SELECT playername FROM 1_toad_circuit /*name of the table*/ LIMIT 0 , 30") ) {
      $row = mysqli_fetch_row($result2);
      printf ("%s \n", $row[0]);
    }

As output I am not getting Itoi6 (person with the best time), but I have Catfish (the first in alphabetical order). 作为输出,我没有得到Itoi6(时间最好的人),但是我有Cat鱼(按字母顺序排列的第一个)。 I have 3 columns as you can see on the picture ( link ). 您可以在图片上看到3列( 链接 )。

I say that I am pretty new with SQL and I would like to know what do I have to fix. 我说我对SQL还是很陌生,我想知道我要修复什么。

You need to add an ORDER BY clause to your statement and then order by whichever field you are storing the times in. 您需要在语句中添加ORDER BY子句,然后按要存储时间的任何字段进行排序。

For example, if you had a "time" field: 例如,如果您有一个“时间”字段:

SELECT playername, time FROM 1_toad_circuit 
ORDER BY `time` ASC 
LIMIT 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM