-1

This is my code:

$sql = "SELECT * FROM servers;";
$result = mysqli_query($conn, $sql);
$datas = array();

if (mysqli_num_rows($result) > 0) {
    while($row = mysqli_fetch_assoc ($result)) {
        $datas[]= $row;
    }
}

This is what the $datas array looks like:

this is how my array looks like

I use the following code and this is what i get:

This is what I want it to look like:

this is how i want my array to look like

Any ideas how I can do this?

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
xyz.sml
  • 5
  • 3

1 Answers1

1

You have a couple of options - remove the id column from the SELECT i.e.

$sql = "SELECT type, host FROM servers";

If you make this change you can simplify the data reading to

$datas = mysqli_fetch_all($result, MYSQLI_ASSOC);

Or you can add only the type and host value into the $datas array:

while($row = mysqli_fetch_assoc ($result)) {
    $datas[]= array('type' => $row['type'], 'host' => $row['host']);
}
Nick
  • 138,499
  • 22
  • 57
  • 95
  • What is the purpose of the loop? It seems to be doing nothing useful at all. You can do that with `fetch_all(MYSQLI_ASSOC)`, right? – Dharman Jan 01 '20 at 22:01
  • @Dharman if the query is changed, then you're right. I've edited. – Nick Jan 01 '20 at 22:19