dpzlz08480 2014-11-29 19:30
浏览 68

Mysql PHP到JavaScript数组

I have a php code returning this data from mysql [8,430], [8,430], [8,380], [8,380], [8,430]

Want to insert it in to this JavaScript data array, cant figure it out how.

<?php
$link = mysql_connect('tankStatus.db.numbers.hostedresource.com', 'username', 'password')
   or die('Could not connect: ' . mysql_error());
     
mysql_select_db('tankStatus') or die('Could not select database');
  
$dataArray=array();
$dateArray=array();
$in=0;
//get data from database
$sql="SELECT * FROM Additives ";
$result = mysql_query($sql) or die('Query failed: ' . mysql_error());
if ($result) {
  while ($row = mysql_fetch_assoc($result)) {
      $dataArray[$in]=$row["Calcium"];
      $dateArray[$in]=$row["pH"];
        $in++;
  }
  $arrayLenght = count($dataArray);
  for($x = 0; $x < $arrayLenght; $x++) {
  echo "{$dateArray[$x]},";
    echo "{$dataArray[$x]}";
     if($x < $arrayLenght-1){
     echo ", ";
     }
  }
}
?>will return 8,400, 8,420, 8,430, 8,430, 8,380, 8,380, 8,430

// Graph Data ##############################################
    var graphData = [{
            // Visits
            data: [php data inserted here],
            color: '#71c73e'
        }, {
            // Returning Visits
            data: [ [6, 500], [7, 600], [8, 550], [9, 600], [10, 800], [11, 900], [12, 800], [13, 850], [14, 830], [15, 1000] ],
            color: '#77b7c5',
            points: { radius: 4, fillColor: '#77b7c5' }
        }
    ];

</div>
  • 写回答

3条回答 默认 最新

  • dongwei3120 2014-11-29 20:52
    关注

    It's best to use JSON to get data from PHP to JavaScript:

    PHP: (get_data.php)

    $formatted_data = array();
    foreach($dataArray as $in => $ph){
        $formatted_data[] = array($in, $ph);
    }
    
    echo(json_encode($formatted_data)); exit();
    

    JavaScript and jQuery:

    $.ajax({
        dataType: "json",
        url: get_data.php,
        success: function(data){
            alert(data);
        }
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c