苗条的应用程序错误数据库
Slim Application Error DB
我是 Restful API 的新手。我正在尝试制作自己的以下 Internet 教程。
为此 API 我正在使用 Slim PHP 框架。
我已经创建了 db.php(配置文件):
<?php
class db {
//properties
private $dbhost = 'localhost';
private $dbuser = 'root';
private $dbpass = '';
private $dbname = 'slimapi';
//Connect
public function connect() {
$mysql_connect_str = "mysql:host=$this->dbhost;dbname=$this->dbname";
$dbConnection = new PDO($mysql_connect_str, $this->dbuser, $this->dbpass);
$dbConnection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return $dbConnection;
}
}
然后我得到了我的 paciente.php(我正在尝试将数据提取到的位置):
<?php
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
$app = new \Slim\App;
//Get All Pacientes
$app->get('/api/pacientes', function (Request $request, Response $response) {
$sql = "SELECT * FROM pacientes";
try {
//GET DB Object
$db = new db();
//Connect
$db = $db->connect();
$stmt = $db->query($sql);
$pacientes = $stmt->fetchAll(PDO::FETCH_OBJECT);
$db = null;
echo json_encode($pacientes);
} catch(PDOException $e) {
echo '{"error": {"text": '.$e->getMessage().'}';
}
});
我在尝试加载 /api/pacientes 时遇到 this 错误:
Slim Application Error
A website error has occurred. Sorry for the temporary inconvenience.
我已经测试了与数据库的连接,没问题。 我可能在配置文件 (db.php) 上做错了,但我知道。
感谢大家的阅读和回答!
编辑:
如果需要,请添加我的 index.php:
<?php
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
require '../vendor/autoload.php';
require '../src/config/db.php';
$app = new \Slim\App;
$app->get('/hello/{name}', function (Request $request, Response $response, array $args) {
$name = $args['name'];
$response->getBody()->write("Hello, $name");
return $response;
});
//Pacientes Routes
require '../src/routes/pacientes.php';
$app->run();
这是一个错字:
pacientes.php
上的第 18 行
$pacientes = $stmt->fetchAll(**PDO::FETCH_OBJECT**);
必须是
$pacientes = $stmt->fetchAll(***PDO::FETCH_OBJ***);
我是 Restful API 的新手。我正在尝试制作自己的以下 Internet 教程。 为此 API 我正在使用 Slim PHP 框架。
我已经创建了 db.php(配置文件):
<?php
class db {
//properties
private $dbhost = 'localhost';
private $dbuser = 'root';
private $dbpass = '';
private $dbname = 'slimapi';
//Connect
public function connect() {
$mysql_connect_str = "mysql:host=$this->dbhost;dbname=$this->dbname";
$dbConnection = new PDO($mysql_connect_str, $this->dbuser, $this->dbpass);
$dbConnection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return $dbConnection;
}
}
然后我得到了我的 paciente.php(我正在尝试将数据提取到的位置):
<?php
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
$app = new \Slim\App;
//Get All Pacientes
$app->get('/api/pacientes', function (Request $request, Response $response) {
$sql = "SELECT * FROM pacientes";
try {
//GET DB Object
$db = new db();
//Connect
$db = $db->connect();
$stmt = $db->query($sql);
$pacientes = $stmt->fetchAll(PDO::FETCH_OBJECT);
$db = null;
echo json_encode($pacientes);
} catch(PDOException $e) {
echo '{"error": {"text": '.$e->getMessage().'}';
}
});
我在尝试加载 /api/pacientes 时遇到 this 错误:
Slim Application Error A website error has occurred. Sorry for the temporary inconvenience.
我已经测试了与数据库的连接,没问题。 我可能在配置文件 (db.php) 上做错了,但我知道。 感谢大家的阅读和回答!
编辑: 如果需要,请添加我的 index.php:
<?php
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
require '../vendor/autoload.php';
require '../src/config/db.php';
$app = new \Slim\App;
$app->get('/hello/{name}', function (Request $request, Response $response, array $args) {
$name = $args['name'];
$response->getBody()->write("Hello, $name");
return $response;
});
//Pacientes Routes
require '../src/routes/pacientes.php';
$app->run();
这是一个错字: pacientes.php
上的第 18 行 $pacientes = $stmt->fetchAll(**PDO::FETCH_OBJECT**);
必须是
$pacientes = $stmt->fetchAll(***PDO::FETCH_OBJ***);