-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?

0

1 Answer 1

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']);
}
Sign up to request clarification or add additional context in comments.

2 Comments

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 if the query is changed, then you're right. I've edited.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.