Home > database >  Display result from SQL using mysqli_fetch_all
Display result from SQL using mysqli_fetch_all

Time:11-30

here is my code:

$sel = $conn->query("SELECT user FROM db_user WHERE level='1'");
$rsel = mysqli_fetch_all($sel, MYSQLI_ASSOC);
echo json_encode($rsel);

so, it will give result : [{"user":"reg-60"},{"user":"reg-76"}]

my question is: how to get the result just name of user? so in browser just show : reg-60, reg76

Thank you for the help!

CodePudding user response:

You may use a foreach loop and then use implode

<?php

$sel = $conn->query("SELECT user FROM db_user WHERE level='1'");
$rsel = mysqli_fetch_all($sel, MYSQLI_ASSOC);

foreach ($rsel as $result){
  $temp[]= $result["user"];
}

echo implode($temp,",");

?>

Note: For your case you are hardcoding the query condition to level='1' and so it will be OK. But if the condition depends on user provided data then you should amend the select query to parameterized prepared statement which is resilient against SQL injection.

  •  Tags:  
  • php
  • Related