简体   繁体   中英

Javascript array from mysql database in php

 var chartData = [{
     "country": "Czech Republic",
     "litres": 156.9
 }, {
     "country": "Ireland",
     "litres": 131.1
 }, {
     "country": "Germany",
     "litres": 115.8
 }, {
     "country": "Australia",
     "litres": 109.9
 }, {
     "country": "Austria",
     "litres": 108.3
 }, {
     "country": "UK",
     "litres": 65
 }, {
     "country": "Belgium",
     "litres": 50
 }];

i want to create this array using mysql database in php how can i do it ?

You can fetch the record from database as array format then it will convert into a json object. Try below code

// fetch the record form mysql table using mysqli_fetch_assoc() and store it in $chartData
    // then use json_decode() function used to convert the array into object format
    $chartData[] = array("country"=> "Czech Republic", "litres"=> 156.9);
    $chartData[] = array("country"=> "Czech Republic", "litres"=> 156.9);
    $chartData[] = array("country"=> "Czech Republic", "litres"=> 156.9);
    $chartData[] = array("country"=> "Czech Republic", "litres"=> 156.9);
    $chartData[] = array("country"=> "Czech Republic", "litres"=> 156.9);
    $chartData[] = array("country"=> "Czech Republic", "litres"=> 156.9);
    echo '<pre>';
    var_dump(json_encode($chartData));

I think it will helpful for you.. :-)

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