0

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.

Daniel
  • 14,004
  • 16
  • 96
  • 156
  • Possible duplicate of [PDOException SQLSTATE\[HY000\] \[2002\] No such file or directory](http://stackoverflow.com/questions/20723803/pdoexception-sqlstatehy000-2002-no-such-file-or-directory) – jmattheis Dec 09 '16 at 06:51
  • So I tried changing from localhost to 127.0.0.1 and I get connection refused. Unfortunately, I have not found a situation where someone was using SQLite with the Slim Framework so this is why I posted my question. – Daniel Dec 09 '16 at 12:39
  • Not a Slim issue. Look at error logs. How does the code which connects to database look like. – Mika Tuupola Dec 10 '16 at 11:55

0 Answers0