I'm trying to build a chart, and I need to get data from a MySQL database table in a JavaScript variable that will be in the following format: var variable1 = [[1, 19], [2, 11], [3, 14], [4, 16]]. The first number (column) becomes x, and the second becomes y on my chart. My table in a 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'm new to this, please excuse me for asking, which can be a very simple question.
EDIT:
Using Wart's answer, I encoded 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>
<script language="javascript" type="text/javascript" src="flot/jquery.flot.js"></script>
<script type="text/javascript">
$.ajax({
url : 'serv.php',
type : 'GET',
success : function(result){
var obj = jQuery.parseJSON(result);
console.log(obj);
}
});
</script>
</head>
<body>
Hi
</body>
</html>
And this is my PHP file called serv.php , which is in the same directory as the HTML file:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "datadb";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT column1, column2 FROM chartdata";
$result = $conn->query($sql);
echo json_encode($result);
?>
, , , null :
Object {current_field: null, field_count: null, lengths: null, num_rows: null, type: null}
?