2015-05-03 20 views
0

我试图用下面的代码从数据库中拉出一些记录,但是我的代码末尾的echo json_encode($contacts);没有打印任何东西。上面没有任何echojson_encode没有得到echo'ed

<?php 
    require_once(dirname(__FILE__).'/ConnectionInfo.php'); 


    //Set up our connection 
    $connectionInfo = new ConnectionInfo(); 
    $connectionInfo->GetConnection(); 

    if (!$connectionInfo->conn) 
    { 
     //Connection failed 
     echo 'No Connection'; 
    } 

    else 
    { 
     //Create query to retrieve all contacts 
     $query = 'SELECT Numero_Leccion,Titulo_Leccion,Ejemplo_Leccion FROM leccion'; 

     $stmt = sqlsrv_query($connectionInfo->conn, $query); 

     if (!$stmt) 
     { 
      //Query failed 
      echo 'Query failed'; 
     } 

     else 
     { 
      $contacts = array(); //Create an array to hold all of the contacts 
      //Query successful, begin putting each contact into an array of contacts 

      while ($row = sqlsrv_fetch_array($stmt,SQLSRV_FETCH_ASSOC)) //While there are still contacts 
      { 
       //Create an associative array to hold the current contact 
       //the names must match exactly the property names in the contact class in our C# code. 
       $contact = array("Numero_Leccion"=>$row['Numero_Leccion'],"Titulo_Leccion"=>$row['Titulo_Leccion'],"Ejemplo_Leccion"=>$row['Ejemplo_Leccion']);    
       //Add the contact to the contacts array 
       array_push($contacts, $contact); 
      } 
      //Echo out the contacts array in JSON format 
      echo json_encode($contacts); 
     } 
    } 
?> 
+2

而你的问题是什么?它什么都不输出,或者'NULL'或者..? – kero

+0

它回声没有什么,如果我放置和回声json_encode($ contatcts)上面的常规回声如果得到那里。并且输出变为空白。另一件事,如果我回应这个“echo json_encode($ contact)”它打印该数组 –

+0

我的查询实际上从数据库中检索到4条记录,我的代码没有回显它。 –

回答

0

error_reporting(E_ALL & ~E_NOTICE); ini_set('display_errors', '1');某处你的脚本的顶部,看看是否有任何错误

(从Why echoing JSON encoded arrays won't produce any output拍摄)

+0

nop没有错误,一切看起来如果我回声json_encode($联系人);回声没什么,如果我反而这样回声json_encode($ contact);就像一次只有一个数组一样,它回响了我想要的json格式的最后3条记录,但与联系人不起作用。 –

0

试试这个:

while ($row = sqlsrv_fetch_array($stmt,SQLSRV_FETCH_ASSOC)) 
{ 
    $contacts[] = array("Numero_Leccion"=>$row['Numero_Leccion'],"Titulo_Leccion"=>$row['Titulo_Leccion'],"Ejemplo_Leccion"=>$row['Ejemplo_Leccion']);    
} 
header('Content-Type: application/json'); 
echo json_encode($contacts); 
+0

它看起来像SQL上的表上的问题,我创建了一个新的像我一样,它的工作,我不知道为什么... ... –