Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 739101
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T08:21:41+00:00 2026-05-14T08:21:41+00:00

Currently I using the following code to get my JSON output from MySQL. <?php

  • 0

Currently I using the following code to get my JSON output from MySQL.

<?php


$session = mysql_connect('localhost','name','pass'); 

mysql_select_db('dbname', $session); 


    $result= mysql_query('SELECT message FROM posts', $session); 
 $somethings = array();  
 while ($row = mysql_fetch_assoc($result)) {  
    $somethings[] = $row; 
 } 

?> 

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

And the output is:

<script type="text/javascript"> 
    var somethings= [{"message":"Welcome to Yo~ :)"},{"message":"Try iPhone post!"},{"message":"????" (the ???? meant to be chinese character)}]; 
</script>

Here is the question, how can I change my output into format like :

<script type="text/javascript"> 
userAge = new Array('21','36','20'),
userMid = new Array('liuple','anhu','jacksen');
</script>

Which I’ll be using later with following code :

 var html = '
<table class="map-overlay">
  <tr>
    <td class="user">' +
      '<a class="username" href="/' + **userMid[index]** + '" target="_blank"><img alt="" src="' +
        getAvatar(signImgList[index], '72x72') +
        '"></a><br>
      <a class="username" href="/' + **userMid[index]** + '" target="_blank">' +
      userNameList[index] +
      '</a><br>
      <span class="info">' + **userSex[index]** + ' ' + **userAge[index]** + '岁<br>
      ' +
      cityList[index] +
      '</span>' +
      '</td>
    <td class="content">' + picString
      + somethings[index] + '<br>
      <span class="time">' +
      timeList[index] + picTips +
      '</span></td>
  </tr>
</table>
'; 

Also how can my json output support chinese character?

Thanks for helping and reading!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T08:21:42+00:00Added an answer on May 14, 2026 at 8:21 am

    Best guessing your question:

    How to change this JSON format:

    [{ key : "Value", key2 : "Value2" }, { key : "Another Value", key2 : "Another Value2" }]
    

    To this format:

    var Keys = ["Value", "Another Value"];
    var Key2s = ["Value2", "Another Value2"];
    

    The answer is: You shouldn’t. JSON is a good data format to work with, and it’s self-contained. If you change what would normally be keys of a JSON object to variables, you’re making your “data format” extremely dependent on where it’s going to be used. Variable name conflicts are almost guaranteed. There’s also no advantage, data in JSON objects is trivial to access already.

    Update:
    You could easily build this format by looping through your results, putting them into arrays and printing out Javascript code, it’s not difficult. Despite that I’m not going to post the code here, since it’s terrible practice. Instead, all you need to do is slightly change the code that will use this data:

    // old
    var html = '...' + key[index] + '...' +key2[index] + '...';
    
    // new
    var html = '...' + data[index].key + '...' + data[index].key2 + '...';
    

    As for Chinese characters, Javascript/JSON is intrinsically UTF8, so it has no problems with Chinese characters. Just make sure your characters are UTF8 encoded when you package them as JSON.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 369k
  • Answers 369k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer If you find you are getting bottlenecked on write time,… May 14, 2026 at 6:24 pm
  • Editorial Team
    Editorial Team added an answer mvn help:evaluate -Dexpression=project.version If you want just the version echoed,… May 14, 2026 at 6:24 pm
  • Editorial Team
    Editorial Team added an answer UPDATE table SET NrDetail = ( Select Count(*) FROM table… May 14, 2026 at 6:24 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.