Php mySQL查询创建的JSON返回未定义但数组相同
正在开发mySql、PHP、jquery web应用程序 现在,我的jQuery脚本可以很好地处理从.php脚本检索到的数据,但是当我从mySQL数据库获得.php脚本中的数据时,我的页面上的值就没有定义,这些值来自数据库 这里是phpPhp mySQL查询创建的JSON返回未定义但数组相同,php,mysql,json,Php,Mysql,Json,正在开发mySql、PHP、jquery web应用程序 现在,我的jQuery脚本可以很好地处理从.php脚本检索到的数据,但是当我从mySQL数据库获得.php脚本中的数据时,我的页面上的值就没有定义,这些值来自数据库 这里是php $get_par ="SELECT hole, par FROM course_card cc LEFT JOIN tour_event_rounds ter ON ter.course_id = cc.course_id WHERE tour_id='
$get_par ="SELECT hole, par FROM course_card cc
LEFT JOIN tour_event_rounds ter ON ter.course_id = cc.course_id
WHERE tour_id='$tid' AND rnd='$rnd' limit 9";
$gp_res = mysql_query($get_par) or die(mysql_error());
$golfer_id=25;
$score_arr[$golfer_id]["name"]="Bob Golfer";
while ($array = mysql_fetch_array($gp_res)) {
$hole = $array['hole']+0;
$score_arr[$golfer_id]["$hole"] = $array['par']+0;
}
$golfer_id=45;
$score_arr[$golfer_id]["name"]="Joe Golfer";
$score_arr[$golfer_id][1]=4;
$score_arr[$golfer_id][2]=3;
$score_arr[$golfer_id][3]=5;
$score_arr[$golfer_id][4]=5;
$score_arr[$golfer_id][5]=5;
$score_arr[$golfer_id][6]=5;
$score_arr[$golfer_id][7]=5;
$score_arr[$golfer_id][8]=5;
$score_arr[$golfer_id][9]=5;
echo json_encode($score_arr);
来自上述php脚本的编码JSON数组输出
{"25":
{"name":"Bob Golfer","1":4,"2":3,"3":4,"4":5,"5":4,"6":5,"7":4,"8":4,"9":3},
"45":
{"name":"Joe Golfer","1":4,"2":3,"3":5,"4":5,"5":5,"6":5,"7":5,"8":5,"9":5}
}
如您所见,Bob的数据孔数据与JSON数组中的Joe相同
但是当我运行js jQuery脚本时。鲍勃的数据没有定义,而乔的数据没有问题。
这怎么可能
这是js
<?php
$hole=1;
$grp=$_REQUEST['grp'];
$tid=$_REQUEST['tid'];
?>
<script type="text/javascript">
tid= <?php echo "$tid"; ?>+0;
grp= <?php echo "$grp"; ?>+0;
rnd= <?php echo "$rnd"; ?>+0;
hole = <?php echo "$hole"; ?>+0;
gid=25;
var score_arr ;
var total = new Array() ;
var total_f = new Array() ;
var total_b = new Array() ;
$(document).ready(function(){
// SET the intial values
$("#hole_num").text(hole);
// Get Data from db
$.getJSON("gse_db_int.php",{"tid":tid,"grp":grp,"rnd":rnd}, function(data){
console.log(data); //uncomment this for debug
$("#score").text(data[gid][hole]);
$("#golfer_name").text(data[gid]["name"]);
score_arr= data;
}); //End json
// show the right view
$("table#one_nine").show();
$("table#ten_plus").hide();
$("table#score_entry").show();
$("table#score_summary").hide();
$("#next_hole").click(function(){
hole=hole+1;
$("#hole_num").text(hole);
$("#score").text(score_arr[gid][hole]);
$("#golfer_name").text(score_arr[gid]["name"]);
});
$("#prev_hole").click(function(){
hole=hole-1;
$("#hole_num").text(hole);
$("#score").text(score_arr[gid][hole]);
});
$(".score_button").click(function() {
score_arr[gid][hole]= parseInt(this.value);
$("#score").text(score_arr[gid][hole]);
});
$("#but_ten_plus").click(function() {
$("table#one_nine").hide();
$("table#ten_plus").show();
});
$("#but_one_nine").click(function() {
$("table#one_nine").show();
$("table#ten_plus").hide();
});
$("table#tab_head").click(function() {
score_summary();
$("table#score_entry").hide();
$("table#score_summary").show();
});
$("tr#sum_head").click(function() {
$("table#score_entry").show();
$("table#score_summary").hide();
});
// build the score summary
function score_summary(){
$("#plug").text("");
$.each(score_arr,function(gid,i){
console.log(gid);
console.log(score_arr[gid]["name"]);
total[gid]=0;
total_f[gid]=0;
total_b[gid]=0;
$("#plug").append('<td> '+score_arr[gid]["name"]+' </td> ');
//FRONT
for(hol=1; hol <= 9 ; hol++){
$("#plug").append('<td> '+score_arr[gid][hol]+' </td> ');
console.log(score_arr[gid][hol]);
total_f[gid]= total_f[gid]+ score_arr[gid][hol];
}
$("#plug").append('<td> '+total_f[gid]+' </td> ');
//BACK
for(hol=1; hol <= 9 ; hol++){
$("#plug").append('<td> '+score_arr[gid][hol]+' </td> ');
console.log(score_arr[gid][hol]);
total_b[gid]= total_b[gid]+ score_arr[gid][hol];
}
$("#plug").append('<td> '+total_b[gid]+' </td> ');
total[gid]= total_f[gid]+total_f[gid];
$("#plug").append('<td> '+total[gid]+' </td> ');
$("#plug").append('</tr> <tr>');
});
}
});
</script>
tid=+0;
grp=+0;
rnd=+0;
孔=+0;
gid=25;
var评分;
var total=新数组();
var total_f=新数组();
var total_b=新数组();
$(文档).ready(函数(){
//设置初始值
$(“#孔数”)。文本(孔);
//从数据库获取数据
$.getJSON(“gse_db_int.php”,{“tid”:tid,“grp”:grp,“rnd”:rnd},函数(数据){
console.log(data);//为调试取消对此的注释
$(“#得分”).text(数据[gid][hole]);
$(“#高尔夫球手姓名”).text(数据[gid][“姓名]);
分数=数据;
});//结束json
//显示正确的视图
$(“表一九”).show();
$(“表十加”).hide();
$(“表#分数项”).show();
$(“表#分数汇总”).hide();
$(“#下一个孔”)。单击(函数(){
孔=孔+1;
$(“#孔数”)。文本(孔);
$(“#分数”).text(分数[gid][hole]);
$(“#高尔夫球手姓名”).text(得分[姓名]);
});
$(“#上一个孔”)。单击(函数(){
孔=孔-1;
$(“#孔数”)。文本(孔);
$(“#分数”).text(分数[gid][hole]);
});
$(“.score_按钮”)。单击(函数(){
score_arr[gid][hole]=parseInt(此.value);
$(“#分数”).text(分数[gid][hole]);
});
$(“#但_十加”)。单击(函数(){
$(“表一九”).hide();
$(“表十加”).show();
});
$(“#但#一#九”)。单击(函数(){
$(“表一九”).show();
$(“表十加”).hide();
});
$(“表#选项卡_头”)。单击(函数(){
得分汇总();
$(“表#分数项”).hide();
$(“表#分数汇总”).show();
});
$(“tr#sum_head”)。单击(函数(){
$(“表#分数项”).show();
$(“表#分数汇总”).hide();
});
//建立分数汇总表
功能得分_总结(){
$(“#plug”).text(“”);
美元。每个(分数、功能(gid、i){
控制台日志(gid);
日志(score_arr[gid][“name”]);
总计[gid]=0;
总数f[gid]=0;
总收入[gid]=0;
$(“#plug”).append(“”+score_arr[gid][“name”]+“”);
//正面
对于(hol=1;hol-Joe's很好,因为您正在使用变量手动定义它。Bob's可能未定义,因为MySQL和MySQL\u fetch\u数组中存在问题。您确定数组获取没有返回错误或返回空值吗?然后,当JSON被编码并发现它未定义时,它可能只是抓住了nearest可用值。不要使用mysql。它已被弃用。使用PDO或mysqli。您也可以添加JQuery部分吗?当我更改bob的db值时,JSON中的数据会更改。添加了脚本和JQuery