开发者

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

开发者 https://www.devze.com 2022-12-27 15:10 出处:网络
Im not sure how to output MySQL data into formats below. (eg: timelist, usersex, userage from table users.)

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

精彩评论

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