kilnakorr Posted October 3, 2019 Share Posted October 3, 2019 Hi Already looked around, and although I've found some solution I simply don't understand them (not skilled at all). I'm getting 10 random results using this, which works fine and list 10 random results. $sql = "SELECT name FROM db ORDER BY RAND() LIMIT 10;"; $result = mysqli_query($con,$sql); while ($row=mysqli_fetch_array($result,MYSQLI_ASSOC)) { echo $row['name']."<br>"; } How can I make 10 new variables for each result? Quote Link to comment https://forums.phpfreaks.com/topic/309324-generate-variables-in-while-loop/ Share on other sites More sharing options...
Barand Posted October 3, 2019 Share Posted October 3, 2019 Use an array. $sql = "SELECT name FROM db ORDER BY RAND() LIMIT 10;"; $result = mysqli_query($con,$sql); $names = []; while ($row=mysqli_fetch_array($result,MYSQLI_ASSOC)) { $names[] = $row['name']; } Now your names are in the variables $names[0], $names[1] , … , $names[9] "db" is a poor name for a table; couldn't you think of anything more meaningful? Quote Link to comment https://forums.phpfreaks.com/topic/309324-generate-variables-in-while-loop/#findComment-1570262 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.