0

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

1 回答 1

1

首先,使用json_encode()而不是构建 JSON 字符串。

其次,在第二个查询中,您的和变量似乎没有WHEREorLIMIT子句(我不知道您要对它们做什么)。$start$amt

第三,不推荐使用 MySQL 扩展。您不应该使用它来编写新代码。相反,使用 PDO(强烈推荐)或 MySQLi。

https://stackoverflow.com/a/12860046/283366

于 2013-01-27T05:59:16.120 回答