如何计算某个数据点上方的表中的行?

时间:2016-07-29 08:41:20

标签: php sql

我已经为他们的分数排序的测验制作了排行榜,我想展示用户的位置。我如何计算表格中有多少行以确定它们的位置?此代码仅适用于人们没有相同分数的情况。如果2人都有高分,他们都会把自己看作2号。

function firstPlace() {
    include 'connection.php';
    $sql="SELECT * FROM Users WHERE Email='$_SESSION[email]'";
    $result=mysqli_query($connection,$sql);
    while($row1=mysqli_fetch_array($result)) {
        if(isset($row1[4])) {
            $score=$row1[4];
        }
    }
    $sql2="SELECT * FROM Users WHERE Email='$_SESSION[email]'";
    $result2=mysqli_query($connection,$sql2);
    $sql1="SELECT * FROM Users WHERE Score >= $score ORDER BY Score DESC";
    $result1=mysqli_query($connection,$sql1);
    $numRows=mysqli_num_rows($result1);

    while($row=mysqli_fetch_array($result2)) {
        if($row[5]!="") {
            $src1="photos/".$row[5];
        }
        else {
            $src1='images/profile-pic.png';
        }
        echo "
        <a  class='logo' href='index.php'><img src='$src1' class='bigProfilePic' id='logo' alt='Profile pic'></a>
        <h1>$numRows</h1>
        <h2>$row[1]</h2>
        <h3>$row[4]<span>Kms</span></h3>";
    }

1 个答案:

答案 0 :(得分:0)

$sql = 'SELECT count(id) FROM Users WHERE Score > ?';
$stmt = mysqli_prepare($sql);
$stmt->bind_param("i", $Score );
$stmt->execute();
$result = $stmt->get_result();

会给你有多少用户得分高于$得分,你当然可以保持查询打开sql注意为其他人如果你想