尝试从数据库中选择随机数< = ticket的用户

时间:2016-12-10 21:07:20

标签: php sql database select mysqli

我试图选择并回应数据库中的用户,他的ticket1< = $ rand和ticket2> = $ rand但它不起作用,我不知道为什么。 这是代码:

<?php
    $sql = "SELECT t1, t2 FROM user";
    $result = mysqli_query($conn, $sql);
    $sqlt = "SELECT t2 FROM user ORDER BY id DESC LIMIT 1";
    $resultt = mysqli_query($conn, $sqlt);
    $rowt = mysqli_fetch_assoc($resultt);
    $rand = rand(1, $rowt['t2']);
    echo $rand.'<br>';
    while($row = mysqli_fetch_assoc($result)){
        echo $row['t2'].'<br>';
        $t1 = $row['t1'];
        $t2 = $row['t2'];
        $sqls = "SELECT uid FROM user WHERE $t1 <= $rand && $t2 >= $rand";
        $results = mysqli_query($conn, $sqls);
        $rows = mysqli_fetch_row($result);
        echo $rows[0];
    }

&GT;

1 个答案:

答案 0 :(得分:0)

首先,我们无法帮助您,因为您没有让我们看到您的餐桌上有什么。

    $sql = "SELECT t1, t2 FROM user"; 
    $result = mysqli_query($conn, $sql);
    //This is for getting t1 and t2 from user table

    $sqlt = "SELECT t2 FROM user ORDER BY id DESC LIMIT 1";
    $resultt = mysqli_query($conn, $sqlt);
    $rowt = mysqli_fetch_assoc($resultt);
    //This is for getting only t2 from last row in user table

    $rand = rand(1, $rowt['t2']);
    //This is for picking up random number from 1 to t2 
    //which is in the last row from user table
    //so it may be lower number than you want
    //if you want to get number of rows its done differently

    while($row = mysqli_fetch_assoc($result)){
        $t1 = $row['t1'];
        $t2 = $row['t2'];
    //Here you are getting every t1 and t2 value for every row

        $sqls = "SELECT uid FROM user WHERE $t1 <= $rand && $t2 >= $rand";
    //And here you are saying I want to select userId from userTable where
    //someNumber <= randomNumber and someOtherNumber>= randomNumber
    //which wont work, you are looking for userId from userTable where 
    //randomNumber is >= than SomeNumber which is t1 and randomNumber<=t2
        $results = mysqli_query($conn, $sqls);
        $rows = mysqli_fetch_row($result);
        echo $rows[0];
    }