简体   繁体   中英

SLIM Application error. Code 8: Array to string conversion

I'm currently working in a mobile application (Front-end) which brings some data through a Php Slim backend from a MySQL database using PDO. This (Back-end) was developed by a team mate and works like a charm on his computer.

There's a GET route which is supposed to return some JSON data:

$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();
});

The 'User' Controller as all of them, inherit from 'CtrlDB' controller.

If I try to access ' api/users ' I get:

Type: ErrorException Code: 8 Message: Array to string conversion File: /home/shy-n/projects/tienda/api/controllers/CtrlDB.php Line: 32

The line 32 is located at the CtrlDB constructor:

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)."`";
}

In " echo $response " I get the error, and I have no clue what's going on.

He uses WAMP server with php 5.5.12

I'm using Arch Linux 64 Bits with LAMP with php 5.6.5. I have enabled both extensions mysqli.so and pdo_mysql.so in my php.ini file.

I have imported the database used with phpmyAdmin, containing the same registers as my friend in the back-end.

In spite of all of this, he can get the JSON data accessing the /users route, but I can't.

Thanks in advance for your help.

instead of

  echo $response;

try

  echo json_encode($response);

you shouldn't be echoing out arrays

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM