-1

I am trying to create a key and value array from database record, but the array just getting the final record! Here is a snippet of my code:

$cateogiresArr = array();
while ($row = mysqli_fetch_row($result))
{
  $cateogiresArr["categoryname"] = $row[1];
  $cateogiresArr["description"] = $row[2];
}
header("Content-type:application/json");
$json_categories = json_encode($cateogiresArr);
0

1 Answer 1

4

On each iteration add new array with required data to $cateogiresArr:

while ($row=mysqli_fetch_row($result))
{
    $cateogiresArr[] = [
        "cateogryname" => $row[1],
        "description" => $row[2],
    ];
}
Sign up to request clarification or add additional context in comments.

1 Comment

@PhillipMark Please mark this answer as accepted by clicking the green tick on the left. Also, some spelling corrections: categoryname, and categoriesArr.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.