SLIM应用程序错误。代码8:数组到字符串转换

时间:2015-02-06 23:49:27

标签: php mysql pdo mysqli slim

我目前正在使用移动应用程序(前端),它通过使用PDO的MySQL数据库的Php Slim后端提供一些数据。这个(后端)是由一个队友开发的,并且在他的计算机上起到了一种魅力。

有一条GET路由应该返回一些JSON数据:

$app->get('/users', function () {
   require_once  'controllers/User.php';
   $user = new User();
   $user->setJsonMode(true);
   $user->setJoin('default');
   $user->setSelect('user_id, user.role_id, role, name,
                     userName, email, picture, user.last_update');
   echo $user->select();
});

'User'Controller作为所有这些,继承自'CtrlDB'控制器。

如果我尝试访问“ api / users ”,我会得到:

类型:ErrorException 代码:8 消息:数组到字符串转换 文件:/home/shy-n/projects/tienda/api/controllers/CtrlDB.php 行:32

第32行位于CtrlDB构造函数:

public function __construct($table,$fields,$idProperty,$relations) {
    $dsn = DB_ENGINE.':host='.DB_HOST.';dbname='.DB_NAME.';charset=utf8';
    try {
        $this->db = new PDO($dsn, DB_USERNAME, DB_PASSWORD, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
    } catch (PDOException $e) {
         $response = $this->response("error","Connection failed: " . $e->getMessage(),null);
         echo $response;
         exit;
    }

    $this->table = $table;
    $this->idProperty = $idProperty;
    $this->fields = $fields;
    $this->relations = $relations;
    $this->start = 0;
    $this->limit = 25;
    $this->select = "`".implode("`, `",$fields)."`";
}

在“ echo $ response ”中,我收到错误消息,我不知道发生了什么。

他使用WAMP服务器和php 5.5.12

我正在使用带有LAMP的Arch Linux 64 Bits和php 5.6.5。我启用了两个扩展程序 我的php.ini文件中的 mysqli.so pdo_mysql.so

我导入了与phpmyAdmin一起使用的数据库,其中包含与后端的朋友相同的寄存器。

尽管如此,他可以获取访问/ users路由的JSON数据,但我不能。

提前感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

而不是

  echo $response;

  echo json_encode($response);

你不应该回应数组