简体   繁体   English

PHP MySQL ORDER BY DESC LIMIT 1

[英]PHP MySQL ORDER BY DESC LIMIT 1

i have a script to show the most watched movie. 我有一个脚本来显示观看次数最多的电影。 This information is from a MySQL Database. 此信息来自MySQL数据库。

This code works: 此代码有效:

<?php

$sql = "SELECT title, time, filename, imageurl, ms, bekeken, genres FROM movies ORDER BY bekeken 
DESC LIMIT 1";

$result = mysqli_query($mysqli, $sql);

if (mysqli_num_rows($result) > 0)
{
//
while($row = mysqli_fetch_assoc($result))
{
            echo '<div class="right-content">
        <div class="popular">
            <h3>Populairste film</h3>
            <p>Meest bekeken film!</p>
            <div class="clear"> </div>
        </div>
        <div class="grid1">
                    <h3>' . $row["title"] . '</h3>
                    <a href="player.php?file=' . $row["filename"] . '&ms=' . $row["ms"] . '"><img src="' . $row["imageurl"] . '" title="' . $row["title"] . '"  height="260" width="200"/></a>
                    <div class="time1">
                        <span>' . $row["time"] . '</span>
                    </div>

                    <div class="grid-info">

                        <div class="video-watch">
                            <a href="#">Kijk nu!</a>
                        </div>
                        <div class="clear"> </div>
                        <div class="lables">
                            <p>Genre:<a href="genres.php?g=">' . $row["genre"] . '</a></p>
                        </div>
                    </div>
                </div>
                <div class="clear"> </div>';
}
}
?>

The problem: 问题:

When the variable "bekeken" is more than 10, it automatically selects the one less than 10 as most watched. 当变量“ bekeken”大于10时,它会自动选择少于10个观看次数最多的变量。

It does not properly select the one where "bekeken" is the highest number. 它没有正确地选择“ beken”为最高数字的数字。

It works perfectly if all the numbers in the database row "numbers" are less than 10. 如果数据库行“数字”中的所有数字均小于10,则可以完美地工作。

How can i fix this? 我怎样才能解决这个问题?

Thanks in advance 提前致谢

It sounds like the problem is that bekeken is not stored as a number, but as a string. 听起来好像问题在于bekeken不是存储为数字,而是存储为字符串。 You can convert it to a number for the sorting: 您可以将其转换为数字以进行排序:

SELECT title, time, filename, imageurl, ms, bekeken, genres
FROM movies
ORDER BY (bekeken + 0) DESC
LIMIT 1

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

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