I decided to practice for the first time the Slim framework and I am not using MAMP or XAMPP, but instead the php -S. So in the process I was trying to figure out how to create a database.
I decided to use SQLite to quickly create a database via command line. I believe I successfully created the tables and inserted data.
I tried to access it via my post.php file:
<?php
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
$app = new \Slim\App;
// Get All Posts
$app->get('/api/posts', function(Request $request, Response $response){
$sql = "SELECT * FROM posts";
try{
// Get Database Object
$db = new db();
// Connect
$db = $db->connect();
$stmt = $db->query($sql);
$posts = $stmt->fetchAll(PDO::FETCH_OBJ);
$db = null;
echo json_encode($posts);
} catch(PDOException $e ){
echo '{"error": {"text":'.$e->getMessage().'}}';
}
});
Unfortunately, in the browser I am getting this error:
{"error": {"text":SQLSTATE[HY000] [2002] No such file or directory}}
I am not sure if the problem is that I need to create the user and grant user privileges in SQLite or something else I may not be aware of. I have never worked with SQLite inside of the Slim Framework. Any direction will be much appreciated.