-3

I wanna ask how to display database usernames from mysql in php echo

    $send = $database->query('SELECT * FROM `usernames` ORDER BY RAND() LIMIT 1');
while ($show = mysql_fetch_array($send)) {
    echo ''.$show['usernames'];
}

3 Answers3

0

This fixes the php error:

$ts = $database->query('SELECT * FROM `usernames` ORDER BY RAND() LIMIT 1');
while ($show = $ts->fetch(PDO::FETCH_ASSOC)) {
    echo ''.$show['usernames'];
}

For sake of completeness you expect only 1 row of results so while-loop could be redundant:

$ts = $database->query('SELECT * FROM `usernames` ORDER BY RAND() LIMIT 1');
$show = $ts->fetch(PDO::FETCH_ASSOC);
echo $show['usernames'];
user3647971
  • 1,069
  • 1
  • 6
  • 13
0
$sql = "SELECT * FROM 'usernames` ORDER BY RAND() LIMIT 1' ";
$result = $conn->query($sql);
while($record = mysqli_fetch_array($result)) {
foreach ($record as $d){
echo $d;
    }
   }
Haseeb
  • 9
  • 2
  • While this code snippet may be the solution, [including an explanation](//meta.stackexchange.com/questions/114762/explaining-entirely-‌​code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – yivi Jan 11 '18 at 14:33
-1

Try this

$query = "SELECT * FROM `usernames` ORDER BY RAND() LIMIT 1"
$send = mysql_query($query);
while ($show = mysql_fetch_array($send)) {
    echo ''.$show['usernames'];
}
Anu
  • 556
  • 6
  • 20