1

I made an API that encodes data from a table to an array then to json.

 <?php
header('Content-type: application/json');
header("Access-Control-Allow-Origin: *");

...(connection to db)... :p

        $query = "SELECT * FROM gdb.".$_GET['tb'];
        $result = mysqli_query($conn,$query);



$posts =array();

while ($row = mysqli_fetch_assoc($result))
{

    $posts[] = array('ONE'=>$row);




}

$all = array('ALL'=>$posts);
echo json_encode($all);//($posts);
mysqli_close($conn);
?>

it seems to work fine with other tables but with this particular table, json_encode doesn't seem t work..

This is the array from the table it won't encode to json:

array(1) {
  ["ALL"]=>
  array(61) {
    [0]=>
    array(1) {
      ["ONE"]=>
      array(12) {
        ["id_product"]=>
        string(1) "2"
        ["id_shop"]=>
        string(1) "1"
        ["id_lang"]=>
        string(1) "1"
        ["description"]=>
        string(72) "<p> BUY 5-KILOS  RICE FREE SARDINES 155G.SAVE 10.00</p>"
        ["description_short"]=>
        string(0) ""
        ["link_rewrite"]=>
        string(20) "5-kilos-rice"
        ["meta_description"]=>
        string(0) ""
        ["meta_keywords"]=>
        string(0) ""
        ["meta_title"]=>
        string(0) ""
        ["name"]=>
        string(40) "5-KILOS RICE FREE SARDINES"
        ["available_now"]=>
        string(0) ""
        ["available_later"]=>
        string(0) ""
      }
    }
    [1]=>
    array(1) {
      ["ONE"]=>
      array(12) {
        ["id_product"]=>
        string(1) "3"
        ["id_shop"]=>
        string(1) "1"
        ["id_lang"]=>
        string(1) "1"
        ["description"]=>
        string(78) "<p>BUY 10-KILOS RICE FREE SARDINES RED 155G.SAVE 20.00</p>"
        ["description_short"]=>
        string(0) ""
        ["link_rewrite"]=>
        string(21) "10-kilos-rice"
        ["meta_description"]=>
        string(0) ""
        ["meta_keywords"]=>
        string(0) ""
        ["meta_title"]=>
        string(0) ""
        ["name"]=>
        string(41) "10-KILOS RICE FREE SARDINES"
        ["available_now"]=>
        string(0) ""
        ["available_later"]=>
        string(0) ""
      }
    }
}}

The arrays from the ones that my code were able to encode to json have the same structure. Just different number of fields and content so I dunno why with this particular array, it wouldn't work.

I figured. Maybe some of the description strings(did not include all in my sample) have quotes, doublequotes, slashes and that's why it can't encode to json so i did this in the while loop:

$posts[] =  array('ONE'=>array_map("addslashes",$row));

instead of this:

$posts[] = array('ONE'=>$row);

but it still doesn't encode to json. Did i use array_map wrong?? or is there another reason why it won't encode to json?

8
  • 2
    json_encode doesn't seems to work? What is the output or exception you get? Commented Jul 29, 2015 at 8:40
  • 6
    You shouldn't need to call addslashes() - json_encode() will handle that. Have you checked json_last_error() or json_last_error_msg() to see if there was a json error? Commented Jul 29, 2015 at 8:40
  • 1
    @Jim I tried json_last_error(); it only says "5" and json_last_error_msg(); says "Malformed UTF-8 characters, possibly incorrectly encoded" :o Commented Jul 29, 2015 at 8:49
  • 1
    @JohannaCristineDy Looks like you have encoding issues. See this post: stackoverflow.com/q/9098507/505722 Commented Jul 29, 2015 at 8:51
  • 1
    @Darragh Okay.. Thank you! I'll see what I can do to make my API more secure. Still new to APIs ^^; My friend uses access tokens. I guess I'll have to study about that. Commented Jul 29, 2015 at 9:00

1 Answer 1

2

I just needed to add this

mysqli_set_charset($conn,"utf8");

before mysqli_select_db() after all :B

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.