客户端按下按钮时在PHP中计数器

I have this part of code in php . when player press button in client (using ajax) I want my database show next record. but I won't.

if(isset($_POST['req'])){

$counter++;

$sql = "SELECT question FROM mytable WHERE id = $counter";

$result = $conn->query($sql);

if ($result->num_rows > 0) {

while($row = $result->fetch_assoc()) {
    echo $row["id"]." ". $row["question"]. " " . "<br>";
}
} else {
 echo "0 results";
}

}

I would suggest storing your counter in a session. Then each time a player does this action you can give them the next row like so :-

session_start();


if(isset($_POST['req'])){

    if ( ! isset($_SESSION['counter']) ) {
        $_SESSION['counter'] = 1;
    } else {
        $_SESSION['counter'] = $_SESSION['counter'] + 1;
    }

    $sql = "SELECT question FROM mytable WHERE id = {$_SESSION['counter']}";
    $result = $conn->query($sql);

    if ( ! $result ) {
        // log error to error log
        error_log(print_r($conn->errorinfo(),true), 3, 'app_error.log');
        echo "Temporary database issues, please try again later";
        header('Location: error_page.php');
        exit;
    }

    if ($result->num_rows > 0) {
        $row = $result->fetch_assoc();
        echo $row["id"]." ". $row["question"]. " " . "<br>";
    } else {
        echo "0 results";
    }
}

The easy way is to send the current id along in the Ajax request. Increment it then use it to pull the next question from your database