jeudi 3 janvier 2019

PHP/MYSQL - Generate only one random word inside for loop

I am making a small interraction between a user and the computer in a game. The user is always the first one to make a move, where he inputs a word of his choise. I did a sql query that gets all plays made by the user while he is playing (maximun of 10 attempts). Once the user makes the first move I want the computer to generate a random word, like this:

$sql="Select word from plays where user=1 and id=$id";
$result=mysqli_query($link, $sqlj);

for($i=0;$i<10 && ($row=mysqli_fetch_assoc($result)) ;$i++){

    // some code here

    $random="select word from words order by Rand() LIMIT 1";
    $result=mysqli_query($link, $random);
    $wrandom=mysqli_fetch_assoc($result);
    $wordR=$wrandom['word']; 
 } 

In the first attempt all works correctly, a random word is generated but in the second attempt two random words are generated (instead of just one), in the third attempt, 3 words are generated and so on... What should I do to generate just one random word per attempt?




Aucun commentaire:

Enregistrer un commentaire