我目前在一个移动应用程序(前端)中工作,该应用程序使用PDO通过Php-Slim后端从MySQL数据库中获取一些数据。这个(后端)是由一位队友开发的,在他的电脑上运行起来很有魅力。
有一个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();
});
"用户"控制器作为所有这些控制器,继承自"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"中,我得到了错误,但我不知道发生了什么。
他使用带有php 5.5.12 的WAMP服务器
我使用的是带有php 5.6.5的LAMP的Arch Linux 64位。我已启用两个扩展mysqli.so和pdo_mysql.so在我的php.ini文件中。
我已经导入了phpmyAdmin使用的数据库,其中包含与我的朋友在后端相同的寄存器。
尽管如此,他可以获得访问/users路由的JSON数据,但我做不到。
提前感谢您的帮助。
而不是
echo $response;
尝试
echo json_encode($response);
您不应该回显数组