簡體   English   中英

PHP中來自MySQL數據庫的Javascript數組

[英]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
 }];

我想在php中使用mysql數據庫創建此數組,我該怎么做?

您可以從數據庫以數組格式獲取記錄,然后它將轉換為json對象。 試試下面的代碼

// 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));

我認為這將對您有所幫助.. :-)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM