Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/470.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaScript不回显来自数据库的值_Javascript_Php - Fatal编程技术网

JavaScript不回显来自数据库的值

JavaScript不回显来自数据库的值,javascript,php,Javascript,Php,我从数据库中获取值的第一件事是毫无问题的。然后我可以回显这些值,但在这些值变为null之后。不知何故,这些值没有通过这一点。这是我的密码 php和mysql部分 $rows = array(); $result = mysql_query("SELECT * FROM kayitlar"); $i=1; while($row = mysql_fetch_array($result)) { $rows []= array( 'id' => $row['id'], 'ad'

我从数据库中获取值的第一件事是毫无问题的。然后我可以回显这些值,但在这些值变为null之后。不知何故,这些值没有通过这一点。这是我的密码

php和mysql部分

$rows = array();
$result = mysql_query("SELECT * FROM kayitlar");
$i=1;
while($row = mysql_fetch_array($result)) {
  $rows []= array(
    'id' => $row['id'],
    'ad' => $row['ad'],
    'saat' => $row['saat'],
  );        

        $i++;


        }
到目前为止没问题。下面是我遇到问题的代码的其余部分

<script type="text/javascript">
  window.onload = function () {
    var chart = new CanvasJS.Chart("chartContainer",
    {
      title:{
        text: "title"    
      },
      animationEnabled: true,
      axisY: {
        title: "Zaman (saat)"
      },
      legend: {
        verticalAlign: "bottom",
        horizontalAlign: "center"
      },
      theme: "theme2",
      data: [

      {        
        type: "column",  
        showInLegend: true, 
        legendMarkerColor: "grey",
        legendText: "saat",
        dataPoints: [      

        {y:<?php echo json_encode($row['ad']);  ?>, label: "<?php echo json_encode($row['saat']);  ?> "},


        ]
      }   
      ]
    });

    chart.render();
  }

  </script> 

window.onload=函数(){
var chart=new CanvasJS.chart(“chartContainer”,
{
标题:{
正文:“标题”
},
animationEnabled:没错,
axisY:{
标题:“扎曼(萨特)”
},
图例:{
垂直排列:“底部”,
水平对齐:“中心”
},
主题:“主题2”,
数据:[
{        
键入:“列”,
showInLegend:是的,
legendMarkerColor:“灰色”,
传奇文本:“萨特”,
数据点:[
{y:,标签:'},
]
}   
]
});
chart.render();
}

在这里,我卡住的
是没有得到任何值您的$rows数组索引数组,它在每个索引中都包含您的键。所以您需要从数组中提取键值对

完成while循环后,添加以下代码

$arr['ad'] = array_column($rows,"ad");
$arr['saat'] = array_column($rows,"saat");
$arr['id'] = array_column($rows,"id");
现在在jS中使用这个

<script type="text/javascript">
  window.onload = function () {
    var chart = new CanvasJS.Chart("chartContainer",
    {
      title:{
        text: "title"    
      },
      animationEnabled: true,
      axisY: {
        title: "Zaman (saat)"
      },
      legend: {
        verticalAlign: "bottom",
        horizontalAlign: "center"
      },
      theme: "theme2",
      data: [

      {        
        type: "column",  
        showInLegend: true, 
        legendMarkerColor: "grey",
        legendText: "saat",
        dataPoints: [      

        {y:<?php echo json_encode($arr['ad']);  ?>, label: "<?php echo json_encode($arr['saat']);  ?> "},


        ]
      }   
      ]
    });

    chart.render();
  }

  </script> 

window.onload=函数(){
var chart=new CanvasJS.chart(“chartContainer”,
{
标题:{
正文:“标题”
},
animationEnabled:没错,
axisY:{
标题:“扎曼(萨特)”
},
图例:{
垂直排列:“底部”,
水平对齐:“中心”
},
主题:“主题2”,
数据:[
{        
键入:“列”,
showInLegend:是的,
legendMarkerColor:“灰色”,
传奇文本:“萨特”,
数据点:[
{y:,标签:'},
]
}   
]
});
chart.render();
}

您的数组是
多维数组
,因此只需使用
array\u column
即可从每个索引中获取
特定列
值并应用
json\u encode()



不要使用不推荐使用且不安全的
mysql.*
-函数。自PHP5.5(2013年)起,它们就被弃用,并在PHP7(2015年)中被完全删除。改用MySQLi或PDO。@MagnusEriksson如果低于php5.5怎么办lol@OliverNi然后他应该真正升级,因为这些版本已经好几年不受支持了。另外,您仍然可以在旧版本上使用MySQLI和PDO
mysql.*
-无论您使用的是什么PHP版本,函数都很差且不安全。可能是“行”
$rows[0]['ad']
<?php echo json_encode(array_column($rows,'ad'));  ?>
<?php echo json_encode(array_column($rows,'saat'));  ?>