0

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?

  • 2
    `json_encode` doesn't seems to work? What is the output or exception you get? – Rvanlaak Jul 29 '15 at 08: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? – Jim Jul 29 '15 at 08:40
  • @Rvanlaak I am expecting something like {"ALL":[{"ONE":{"id_product":"2","id_supplier":"3","id_manufacturer":"0","id_category_ ... which it generated when i used this code on a different table :o – Johanna Cristine Dy Jul 29 '15 at 08:44
  • 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 – Johanna Cristine Dy Jul 29 '15 at 08:49
  • 1
    @JohannaCristineDy Looks like you have encoding issues. See this post: http://stackoverflow.com/q/9098507/505722 – Jim Jul 29 '15 at 08:51
  • @JohannaCristineDy unrelated but you also should review your db query `"SELECT * FROM gdb.".$_GET['table'];` - it's very vulnerable to SQL injection because you are accepting direct, unsanitised user input. – Darragh Enright Jul 29 '15 at 08:58
  • @Jim Thanks Jim! ^u^ I just needed to add mysqli_set_charset($conn,"utf8"); after all :D – Johanna Cristine Dy Jul 29 '15 at 08:58
  • 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. – Johanna Cristine Dy Jul 29 '15 at 09:00

1 Answers1

2

I just needed to add this

mysqli_set_charset($conn,"utf8");

before mysqli_select_db() after all :B