如何从JavaScript中的MySQL数据库获取数据以构建图表? [英] How to fetch data from MySQL database in JavaScript to build a chart?

查看:210
本文介绍了如何从JavaScript中的MySQL数据库获取数据以构建图表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建图表,我需要从MySQL数据库表中获取数据到JavaScript变量中,该变量的格式应为:var variable1 = [[1, 19], [2, 11], [3, 14], [4, 16]].我图表上的第一个数字(列)为 x ,第二个数字为 y .我在MySQL数据库中的表看起来像这样(我简化了一点):

I am trying to build a chart and I need to fetch data from the MySQL database table into a JavaScript variable that would be in the following format: var variable1 = [[1, 19], [2, 11], [3, 14], [4, 16]]. The first number(column) becomes x and the second is y on my chart. My table in MySQL database looks like this (I simplified it a bit):

column1 column2
   1      19
   2      11
   3      14
   4      16

最简单的方法是什么?我对此并不陌生,请原谅询问可能是一个非常简单的问题.

What is the easiest way to do this? I am new to this, please excuse me for asking what might be a very simple question.

借助Wartus的答案,我编写了如下代码. 我制作了两个文件:带有JavaScript的HTML和一个PHP文件.这是我的HTML文件:

With the help of Wartus' answer I coded as follows. I made two files: HTML with JavaScript and a PHP file. Here is my HTML file:

<html>
<head>
    <title>Title</title>
    <link rel="stylesheet" type="text/css" href="css/common.css" />
    <script language="javascript" type="text/javascript" src="flot/jquery.js"></script>       <!-- jQuery library -->
    <script language="javascript" type="text/javascript" src="flot/jquery.flot.js"></script>  <!-- Library with charts that I plan to use -->
    <script type="text/javascript">

    $.ajax({
       url : 'serv.php', // my php file
       type : 'GET', // type of the HTTP request
       success : function(result){ 
          var obj = jQuery.parseJSON(result);
          console.log(obj);
       }
    });

    </script>   
</head>
<body>
Hi
</body>
</html>

这是我的名为 serv.php 的PHP文件,与HTML文件位于同一目录中.

And this is my PHP file named serv.php that is located in the same directory as the HTML file:

<?php
    $servername = "localhost";
    $username = "root";
    $password = "";
    $dbname = "datadb";

    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    } 

    $sql = "SELECT column1, column2 FROM chartdata";  //This is where I specify what data to query
    $result = $conn->query($sql);
    echo json_encode($result);
?>

这一切似乎都没有错误,只是我在检查控制台时给出了空值:

It all seems to be working without errors except that it gives out nulls when I check console:

Object {current_field: null, field_count: null, lengths: null, num_rows: null, type: null}

我在做什么错了?

推荐答案

在数据库中进行选择后,您必须以json格式返回答案(对我来说,我只是用要测试的值制作了一个数组) :

After you have make your select in your DB you have to return the answer in json format (for me I have just make an array with the value to test) :

您的php文件(我是serv.php):

Your php file (me is serv.php) :

$data = array([1, 19], [2, 11], [3, 14], [4, 16]);
// replace $data by your code to select in DB
echo json_encode($data);

现在,您必须在javascript代码中获得响应.为此,您必须使用javascript或jQuery(在我的情况下为jQuery)发出"GET"请求:

Now you have to get the response in your javascript code. To do that you have to make a "GET" request in javascript or jQuery (jQuery in my case) :

这是您的js文件:

$.ajax({
   url : 'serv.php', // your php file
   type : 'GET', // type of the HTTP request
   success : function(data){
      var obj = jQuery.parseJSON(data);
      console.log(obj);
   }
});

obj中,您拥有数据:

所以现在您拥有数据并可以访问,它是一个数组,因此:

So now you have your data and to access, is an array so :

 - obj[0] contains [1, 19], obj[0][0] contains 1 and obj[0][1] contains 19
 - obj[1] contains [2, 11], obj[1][0] contains 2 and obj[1][1] contains 11 ...

在您的情况下,variable1obj

修改 使用您的数据库:

在发送答案之前,您必须正确构建数据.因此,在您的情况下,您有一个多维数组,当我将数组推入名为data的数组时,该数组就构成了.

Before to send the answer, you have to build correctly your data. So in your case, you have a multidimensional array, that what i make when I push an array in the array named data.

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "datadb";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT column1, column2 FROM chartdata";  //This is where I specify what data to query
$result = mysqli_query($conn, $sql);

$data = array();
while($enr = mysqli_fetch_assoc($result)){
    $a = array($enr['column1'], $enr['column2']);
    array_push($data, $a);
}

echo json_encode($data);

这篇关于如何从JavaScript中的MySQL数据库获取数据以构建图表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆