dimanche 7 février 2021

PHP MySQL Randomize three columns together?

I've written a basic name randomizer (below) which works fine to pull 3 random first names.

<?php
    $sql = "
    SELECT * 
      FROM newNamesTable 
     WHERE newNamesId 
     ORDER 
        BY rand() 
     LIMIT 3;
    ";
    $result = mysqli_query($conn, $sql);                                          
    $rowCount = mysqli_num_rows($result);
    
    if ($rowCount > 0) {
        while($row = mysqli_fetch_assoc($result)){
            echo "<p class=\"resultname\">" . $row['firstNames'] . "</p>";
        }
    } else {
        echo "No results found.";
    } 
?>

However, my table contains middle and last names as well. I am trying to write a code that pulls 3 random entries from each column and arranges them together. I've tried several variations of the code, but can't seem to wrap my head around it.

As a rough table example:

| newNamesId | firstNames | midNames | lastNames |
| ---------- | ---------- | -------- | --------- |
| 1          | Steve      | James    | Doe       |
| 2          | Dave       | John     | Ray       |
| 3          | Pete       | Jim      | Me        |
| 4          | Paul       | Jason    | Fa        |
| 5          | Bill       | Justin   | So        |

I would like the code to mix these names together, so a result might be "Bill James Me" or "Paul Jim So"

Thanks in advance!




Aucun commentaire:

Enregistrer un commentaire