r链接mysql获取数据绘制图表,如何从JavaScript中的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.

EDIT:

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:

Title

$.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);

}

});

Hi

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

$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}

What am I doing wrong?

解决方案

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) :

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

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) :

This is your js file :

$.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);

}

});

And in obj you have your data :

axrke.png

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 ...

In your case, variable1 is the same as obj

Edit

With your DB :

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值