json_encode($ data)= NULL

时间:2018-04-05 14:07:08

标签: php json

也许你可以帮助我。数据库连接正常(Connected successfully),表名正确(course_details),但echo json_encode($data);NULL

<?php

   include "db.php";

   // Check connection
   if ($con->connect_error) {
      die("Connection failed: " . $conn->connect_error);
   } 
   echo "Connected successfully";

   $data=array();
   $q=mysqli_query($con,"'SET CHARACTER SET utf8' select * from 'course_details'");
   while ($row=mysqli_fetch_object($q)){
      $data[]=$row;
   }


   echo json_encode($data); 
   $conn->close();
?>

提前致谢!

1 个答案:

答案 0 :(得分:-1)

首先,我建议将查询包装在try catch块中

其次如上面的评论中所提到的,你在字符串中有2个查询。

then