致命错误:找不到“Services_JSON”类

时间:2013-12-25 10:05:46

标签: php mysql

我尝试在000webhosting上创建一个非常简单的Web应用程序,但是当我尝试实现它时:

<?php

include_once("JSON.php");
$json = new Services_JSON();

$link = mysql_pconnect("******", "******", "******") or die("Could not connect");
mysql_select_db("******") or die("Could not select database");

$arr = array();

$rs = mysql_query("SELECT * FROM users");
while($obj = mysql_fetch_object($rs)) {
    $arr[] = $obj;
}

Echo $json->encode($arr);

?>

但是我收到了这个警告/错误。你能帮帮我吗?

  

警告:include_once(JSON.php)[function.include-once]:失败   open stream:没有这样的文件或目录   第3行/home/a1622045/public_html/index.php

     

警告:include_once()[function.include]:打开'JSON.php'失败   包含(include_path ='。:/ usr / lib / php:/ usr / local / lib / php')in   第3行/home/a1622045/public_html/index.php

     

致命错误:找不到类'Services_JSON'   第4行/home/a1622045/public_html/index.php

2 个答案:

答案 0 :(得分:9)

改变这个:

Echo $json->encode($arr);

到此:

echo json_encode($arr);

并删除此内容:

include_once("JSON.php");
$json = new Services_JSON();

您正在使用的程序使用旧的PEAR库,它将JSON对象表示法转换为PHP数组,反之亦然。然而,PHP已经能够在本地执行多年,因此您的代码依赖于它不需要的依赖。

我已将Echo切换为echo - PHP关键字可能以混合大小写格式运行,但根据手册,以小写形式编写它们是一个很好的约定。

答案 1 :(得分:0)

正如@ Krish-r所说,Services_JSON是PEAR包为JSON表示法提供了一个简单的编码器和解码器。

  1. 无法打开流:验证JSON.php是否可读且与脚本相同[/ li>]
  2. &#39; Services_JSON&#39;未找到:如果尚未安装,请从pear lib(https://pear.php.net/package/Services_JSON)安装Services_JSON,而不是加载&#34; JSON.php&#34;我看起来也做同样的事情。
  3. 为此,请使用[1]:

      

    pear install Services_JSON

    现在不需要include_once(&#34; JSON.php&#34;)。

    [1] https://pear.php.net/manual/en/guide.users.commandline.installing.php