我已尝试将sql结果显示在数据表中,但是当我尝试执行代码时,数据数组的JSON结果显示为NULL。我检查SQL它是正确的但不能以JSON格式显示。这是代码:
<?php
// { initialise variables
$amt=100;
$start=0;
// }
// { connect to database
function dbRow($sql){
$q=mysql_query($sql);
$r=mysql_fetch_array($q);
return $r;
}
function dbAll($sql){
$q=mysql_query($sql);
while($r=mysql_fetch_array($q))$rs[]=$r;
return $rs;
}
mysql_connect('localhost','root','unu@12');
mysql_select_db('GRBS_SITE_DB');
// }
// { count existing records
$r=dbRow('select count(University_Name) as c
from grbs_country join grbs_rating
join grbs_university on grbs_country.Country_Code=grbs_university.Country_Code
and grbs_rating.University_id=grbs_university.University_id ');
$total_records=$r['c'];
// }
// { start displaying records
echo '{"iTotalRecords":'.$total_records.',
"iTotalDisplayRecords":'.$total_records.',
"aaData":[';
$rs= dbAll("select University_Name,Country_Name,Total_Pubs,Per_Pubs_A1,Per_Pub_A1_A,
Total_Cite,Per_Cite_A1,Per_Cite_A1_A,H_Index
from grbs_country join grbs_rating join grbs_university
on grbs_country.Country_Code=grbs_university.Country_Code
and grbs_rating.University_id=grbs_university.University_id
$start,
$amt");
$f=0;
foreach($rs as $r){
if($f++) echo ',';
echo '["',$r['University_Name'],'",
"',$r['Country_Name'],'",
"',$r['Total_Pubs'],'",
"',$r['Per_Pubs_A1'],'",
"',$r['Per_Pub_A1_A'],'",
"',$r['Total_Cite'],'",
"',$r['Per_Cite_A1'],'",
"',$r['Per_Cite_A1'],'",
"',$r['H_Index'],'"]';
}
echo ']}';
// }
萤火虫的结果是这样的:
{"iTotalRecords":189540,
"iTotalDisplayRecords":189540,
"aaData":[]}
答案 0 :(得分:1)
首先,使用json_encode()
而不是构建JSON字符串。
其次,您的第二个查询中的WHERE
和LIMIT
变量似乎没有$start
或$amt
条款(我不知道是什么你正试图与他们合作。)
第三,MySQL扩展已被弃用。您不应该使用它来编写新代码。相反,使用PDO(强烈推荐)或MySQLi。