我尝试将 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":[]}