Basically I want to query MySQL database using an external PHP script. I want this script to be called every 2 seconds. This 2 seconds interval are initiated with a javascript (jquery, flot), which is as follows:
基本上我想使用外部PHP脚本查询MySQL数据库。我希望每2秒调用一次这个脚本。这个2秒的间隔是用javascript(jquery,flot)启动的,如下所示:
<script type='text/javascript'>
var data = [];
var dataset;
var totalPoints = 50;
var updateInterval = 1000;
var now = new Date().getTime();
function GetData() {
data.shift();
while (data.length < totalPoints) {
var y;
$.ajax({
url: 'current_api.php',
success: function(currentValue){
y = currentValue;
console.log(y);
},
});
var temp = [now += updateInterval, y];
data.push(temp);
}
}
var options = {
...
}
$(document).ready(function () {
GetData();
dataset = [
{ label: "CURRENT READING", data: data }
];
$.plot($("#flot-line-chart"), dataset, options);
function update() {
GetData();
$.plot($("#flot-line-chart"), dataset, options)
setTimeout(update, updateInterval);
}
update();
});
</script>
Currently, Im getting a NULL
value at console.log(y);
. My PHP script (current_api.php
) that handles MySQL queries is as simple as follows:
目前,我在console.log(y);获得NULL值。我处理MySQL查询的PHP脚本(current_api.php)非常简单如下:
<?php
require "dbCon.php"; // database credentials
$databaseName = "MAIN";
$tableName = "day"
// OPEN MYSQL DATABASE CONNECTION IN PHP
$dbs = mysql_select_db($databaseName, $con);
// FETCH DATA FROM MYSQL DATABASE
$sql = "SELECT value FROM $tableName ORDER BY id DESC LIMIT 1";
$result = mysql_query($sql);
header('Content-Type: application/json');
while ($row = mysql_fetch_assoc($result)) {
$currentValue = (int) round($row['value']);
}
echo json_encode($currentValue);
// CLOSE THE DB CONNECTION
mysql_close($con);
?>
I'm new with AJAX and does not know if what I'm trying to do is possible. Can someone help me to debug why i'm getting a NULL
value? Thanks in advance.
我是AJAX的新手,不知道我想做的事情是否可行。有人可以帮我调试为什么我得到一个NULL值?提前致谢。
3 个解决方案
#1
1
You are calling current_api.php
in your ajax script without any data. So there is no query string, no $_GET['dbSelect']
and no database. So your json contains only an undefined variable, NULL
.
您在ajax脚本中调用current_api.php而没有任何数据。所以没有查询字符串,没有$ _GET ['dbSelect']而没有数据库。所以你的json只包含一个未定义的变量NULL。
Apart from that this is not correct, you cannot use escaping functions to clean up a user-provided table name, you need to check it against a whitelist.
除此之外,这是不正确的,您不能使用转义函数来清理用户提供的表名,您需要根据白名单进行检查。
#2
0
Got it!
the problem is the declaration of variable y
and the url in $.ajax
. All thanks to barmar and jeroen for the hints!
问题是变量y的声明和$ .ajax中的url。一切都归功于barmar和jeroen的提示!
The javascript should be:
javascript应该是:
<script type='text/javascript'>
var data = [];
var dataset;
var totalPoints = 50;
var updateInterval = 1000;
var now = new Date().getTime();
var y;
function GetData() {
data.shift();
while (data.length < totalPoints) {
$.ajax({
url: 'current_api.php?dbSelect=R9640E5F1E2',
success: function(currentValue) {
y = currentValue;
console.log(currentValue);
},
});
var temp = [now += updateInterval, y];
data.push(temp);
}
}
var options = {
...
}
$(document).ready(function () {
GetData();
dataset = [
{ label: "CURRENT READING", data: data }
];
$.plot($("#flot-line-chart"), dataset, options);
function update() {
GetData();
$.plot($("#flot-line-chart"), dataset, options)
setTimeout(update, updateInterval);
}
update();
});
</script>
where R9640E5F1E2
is the database; and the PHP stays as is. :) Now.. move on to another problem... Query strings inside javascripts.
其中R9640E5F1E2是数据库; PHP保持原样。 :)现在..转到另一个问题...查询javascripts内的字符串。
#3
-1
$.ajax({
dataType: "json",//insert line: receive data from server to json
url: 'current_api.php?dbSelect=123', //a method
/* a method other
type:"GET",
data:{dbSelect:dbSelect},
url: 'current_api.php',
*/
success: function(currentValue){
y = currentValue[0];///edit code y = currentValue
console.log(y);
},
});
#1
1
You are calling current_api.php
in your ajax script without any data. So there is no query string, no $_GET['dbSelect']
and no database. So your json contains only an undefined variable, NULL
.
您在ajax脚本中调用current_api.php而没有任何数据。所以没有查询字符串,没有$ _GET ['dbSelect']而没有数据库。所以你的json只包含一个未定义的变量NULL。
Apart from that this is not correct, you cannot use escaping functions to clean up a user-provided table name, you need to check it against a whitelist.
除此之外,这是不正确的,您不能使用转义函数来清理用户提供的表名,您需要根据白名单进行检查。
#2
0
Got it!
the problem is the declaration of variable y
and the url in $.ajax
. All thanks to barmar and jeroen for the hints!
问题是变量y的声明和$ .ajax中的url。一切都归功于barmar和jeroen的提示!
The javascript should be:
javascript应该是:
<script type='text/javascript'>
var data = [];
var dataset;
var totalPoints = 50;
var updateInterval = 1000;
var now = new Date().getTime();
var y;
function GetData() {
data.shift();
while (data.length < totalPoints) {
$.ajax({
url: 'current_api.php?dbSelect=R9640E5F1E2',
success: function(currentValue) {
y = currentValue;
console.log(currentValue);
},
});
var temp = [now += updateInterval, y];
data.push(temp);
}
}
var options = {
...
}
$(document).ready(function () {
GetData();
dataset = [
{ label: "CURRENT READING", data: data }
];
$.plot($("#flot-line-chart"), dataset, options);
function update() {
GetData();
$.plot($("#flot-line-chart"), dataset, options)
setTimeout(update, updateInterval);
}
update();
});
</script>
where R9640E5F1E2
is the database; and the PHP stays as is. :) Now.. move on to another problem... Query strings inside javascripts.
其中R9640E5F1E2是数据库; PHP保持原样。 :)现在..转到另一个问题...查询javascripts内的字符串。
#3
-1
$.ajax({
dataType: "json",//insert line: receive data from server to json
url: 'current_api.php?dbSelect=123', //a method
/* a method other
type:"GET",
data:{dbSelect:dbSelect},
url: 'current_api.php',
*/
success: function(currentValue){
y = currentValue[0];///edit code y = currentValue
console.log(y);
},
});