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