Get all "username" and "id" in MySQL database and split using php

sorax :

I have MySQL database like this.

id........username  
 1 .......rap
 2 .......dafy
 3 .......kay

I want to make them like thies

username-id,username-id,use...

for ex.

rap-1,dafy-2,kay-3

the code should use the param to prevent the sql injection, but i have no idea how to write code.

DontBe3Greedy :
<?php
$mysqli = new mysqli("database","my_user","my_password","my_db");

if ($mysqli -> connect_errno) {
  echo "Failed to connect to MySQL: " . $mysqli -> connect_error;
  exit();
}

// Obviously change this to match yours
$sql = "SELECT GROUP_CONCAT(CONCAT(username , '-',id) SEPARATOR ',') as grp FROM table";
$result =  $mysqli->query($sql);
$final_result = []
while($row = $result->fetch_assoc())
{
    array_push($final_result, $row['grp']);
}


$mysqli -> close();
?>

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=8427&siteId=1