简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM