PHP converts database query results to JSON format

Source: Internet
Author: User
The database query results are as follows:
To convert it to JSON in the following format, the first attribute is the UNIX timestamp for the date, and the second is the sales

 
  0group by outdate "; $query = Mssql_query ($sql _initbest), while ($row =mssql_fetch_array ($query)) {Array_push ($result [" Data "], $row);} echo Json_encode ($result);? >

Do not know how to turn the date format, the JSON format is not to ask the great God


Reply to discussion (solution)

How is the value of the sales column smaller?

How is the value of the sales column smaller?


It's not changing ... I don't know how to stitch the JSON.
I am now stitching strings but the date format is not and required,
 
  0group by outdate "; $query = Mssql_query ($sql _initbest), $items =array (); echo" {' Data ': "; while ($row =mssql_fetch_array ( $query) {echo "[". Strtotime ($row ["outdate"]). ",". $row ["Sales"]. "],\n";} echo "]}";? >

while ($row =mssql_fetch_array ($query)) {    $result ["Data"] [] = Array (  strtotime ($row ["outdate"]). ' $row ["Sales"]);} echo Json_encode ($result);

To be a hard-to-come? It?

$arr = Array (' data ' = = = Array (' 1161043200000,74.29 '), Array (' 1161043200001,74.53 '), Array (' 1161043200002,78.99 '), Array (' 1161043200003,79.95 ')); Echo Json_encode ($arr);

while ($row =mssql_fetch_array ($query)) {    $result ["Data"] [] = Array (  strtotime ($row ["outdate"]). ' $row ["Sales"]);} echo Json_encode ($result);


Format is not correct, the timestamp and the sales requirement are numeric types.

It's impossible! Post it and remember to output the results

In JSON format specification, both the key and the value are enclosed in double quotation marks (although numeric types are not allowed in quotation marks)
So you can't literally judge the data type of a number string.

If you want numbers, you can use Floatval to turn them into numbers.

while ($row =mssql_fetch_array ($query)) {    $result ["Data"] [] = Array (  floatval (Strtotime ($row ["outdate"]). ' Floatval ($row ["Sales"]);} echo Json_encode ($result);

What's the output of this? $result ["Data"]

Thank you all!

while ($row =mssql_fetch_array ($query)) {$result ["data"] [] = Array (  strtotime ($row ["outdate"]). ' Ceil ($row [' Sales ']);} echo Json_encode ($result);

The result is:


Highcharts use is to remove the double quotation marks from the JSON.
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.