简体   繁体   中英

trying to select a user from database where random number <= tickets

Im trying to select and echo a user in the database that his ticket1 <= $rand and ticket2 >= $rand but its not working and i don't know why. This is the code:

<?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];
    }

?>

First of all we can't help you because you didn't let us see what do you have in your table.

    $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];
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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