开发者

How should i output my MySQL data into json in this kind format?

Im not sure how to output MySQL data into formats below. (eg: timelist, usersex, userage from table users.)

<script type="text/javascript"> 
timeList = new Array(),
userSex = new Array('female','male','male'),
userAge = new Array('21','36开发者_运维百科'),
userMid = new Array('liuple','anhu');
</script>

Thanks!


Use json_encode to output a PHP array into JavaScript literal format.

<?php
    $result= mysql_query('SELECT DISTINCT something FROM sometable ORDER BY something');
    $somethings= array();
    while ($row= mysql_fetch_assoc($results)) {
        $somethings[]= $row['something'];
    }
?>

<script type="text/javascript">
    var somethings= <?php echo json_encode($somethings, JSON_HEX_TAG); ?>;
</script>

This gives you array literal ["anhu", "liuple"] format rather than new Array(). You don't generally want to use new Array() today.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜