0

I have a MySQL table with cols | btn_id | btn_title | btn_bg | btn_text |.

I am trying to get the data in the table to a php array and then return via JSON so the array can be used in the JS document requesting the PHP/MySQL Data. Respective of row and columns/index.

So far i have:

$sql = 'SELECT *
         FROM btn_color_presets
         ';

$result = mysqli_query($sql);

$array = array(); // 

while($row = mysql_fetch_assoc($result)) // 
{
     $array[] = $row;
     $index++;
}

Q. Now i wish to return a JSON Array made from the array of data. How do i proceed here?

Note: I am horrible with arrays and not entirely sure i have the correct method above for my requirements, but i think it is correct.

1
  • 2
    You don't need $index, just use $array[] = $row to add a new element to an array. Commented Jul 13, 2014 at 19:03

1 Answer 1

2

Call json_encode after the loop:

header("Content-type: application/json");
echo json_encode($array);
Sign up to request clarification or add additional context in comments.

1 Comment

Ah brilliant so i was along the right lines. Thanks Barmar!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.