'Create a single comma separated list to echo from mysqli results [duplicate]

$get_top10 = mysqli_query($con, "SELECT name, daily_points, daily_win FROM players WHERE daily_points > 0 ORDER BY daily_points DESC LIMIT 10");

I am needing a string like: name, daily_points, daily_win, name, daily_points, daily_win (etc up to 10 records)

php 7.4.27 mysql 5.6.51-cll-lve

This did the trick, let me know if there is a better or preferred method.

$sql = ("SELECT name, daily_points, daily_win FROM players WHERE daily_points > 0 ORDER BY daily_points DESC LIMIT 10");
        $result = mysqli_query($con, $sql);

        if (mysqli_num_rows($result)) {
            while ($row = mysqli_fetch_assoc($result)) {
                echo "{$row['name']},{$row['daily_points']},{$row['daily_win']},";
            }
        }


Solution 1:[1]

you can try this code it will concatenate your php variable to html ',' and it's a preferred method

  $sql = ("SELECT name, daily_points, daily_win FROM players WHERE daily_points > 0 ORDER BY daily_points DESC LIMIT 10");
            $result = mysqli_query($con, $sql);
    
            if (mysqli_num_rows($result)) {
                while ($row = mysqli_fetch_assoc($result)) {
                     echo $row['name'].','.$row['daily_points'].','.$row['daily_win'];
                }
            }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dotsquares