wita
wita

Reputation: 37

json value returning null from mysql varchar

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

Upvotes: 2

Views: 427

Answers (1)

user2733082
user2733082

Reputation:

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.

Upvotes: 1

Related Questions