2

I'm trying to get the data from mysql using json. the field type is Varchar(56). this is my php codes :

<?php
    $link = mysql_connect('localhost', 'root', '') or die('Cannot connect to the DB');
    mysql_select_db('tugas_akhir', $link) or die('Cannot select the DB');

    /* grab the posts from the db */

    $query = "SELECT ekuivalen
        FROM temp_hasil where username='Dia'";
    $result = mysql_query($query, $link) or die('Errorquery:  '.$query);

    $rows = array();
    while ($r = mysql_fetch_assoc($result)) {
        $rows[] = $r;
    }

    $data = "{aturan:".json_encode($rows)."}";
    echo $data;

    ?>

when I run it on firefox it shows

{aturan:[{"ekuivalen":null}]}

thanks for your help

5
  • is your request returning something in phpmyadmin? Commented May 6, 2014 at 16:00
  • the query? yes. it return something and is working fine in phpmyadmin Commented May 6, 2014 at 16:06
  • you can try json_encore(array('aturan" => $rows)); insteand of your $data = .. line, buti donrt think it will resolve your problem Commented May 6, 2014 at 16:08
  • Insert print_r($rows) right after the while loop and see what happens Commented May 6, 2014 at 17:34
  • var_dump($rows, count($rows), json_encode($rows)); will, i suspect, help to see what is not going well. Commented May 6, 2014 at 18:51

1 Answer 1

1

I had the same issue and discovered the problem is when encoding to json: my varchar field had an è (Unicode char) that seemed causing the problem, so I replaced it with e' (ASCII chars) and now it works, so...

you should check if there are some non ASCII chars in your database fields and replace it.

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.