Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fetch data from MySQL database in JavaScript to build a chart? [closed]

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:

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

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}

What am I doing wrong?

like image 516
Arthur Tarasov Avatar asked Feb 17 '16 08:02

Arthur Tarasov


1 Answers

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 :

enter image description here

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);
like image 163
Berserk Avatar answered Oct 13 '22 00:10

Berserk