Database with slim

hi, sorry my english is not good. that is my problem:
i want to make a form server with slim, and json. when an user want to fill out a form, the server most return to him the data about an user from the database. when the user finished, the data most be seva into the database.
for exemple:
user: get /form
serveur : nom:"" prenom: “”

i dont know how to begin. how can i fill out name without use a form with graphical interface.

thank for your help. I begun slim since one week.

@troy, If you haven’t already, read through the First Tutorial Walkthrough. It provides a good example of how to connect Slim to a database, save and retrieve data, etc.

1 Like

thank you. I will check it.

@troy most of the silex providers are usable within slim3 thanks to pimple container, if you like doctrine dbal (http://www.doctrine-project.org/projects/dbal.html) you can check http://silex.sensiolabs.org/doc/master/providers/doctrine.html

@tflight, I read through the First Tutorial Walkthrough, but I am still unclear as to how to create the database. I am not using MAMP or XAMPP, but connecting to the server via php -S, so how do I get a database up and running using PDO? In other words, do I put the code snippets suggested in the config file and this will eventually connect to MySQL? And what is the MySQL port to use if I were to use Sequel Pro? Thanks in advance.

@ldco2016 The answers to those questions depend on how you installed MySQL. MySQL typically listens on port 3306, but for local development you might use a socket connection.

Do you have MySQL installed?

@tflight, I did not install MySQL, I believe I am using SQLite.

It has been a long time since I’ve used SQLite. I believe you just initialize the database (an empty text file) using the command line. So something like $ touch db.sqlite3 and then load it through PDO as $db = new PDO("sqlite:/path/to/db.sqlite3");

I’m not sure if Sequel Pro has the ability to connect to SQLite databases.

1 Like

So I was able to verify that I have a database with tables in SQLite3 but I am still getting this error:

{“error”: {“text”:SQLSTATE[HY000] [2002] No such file or directory}}

If that is the case, the error message suggests either the path or the file name is incorrect.

hmm, could it be that since i am using the php server and sqlite3 that the way I coded this is wrong?

db.php:

<?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().'}}'; } });

If you are getting the same error message, I would still be inclined to believe the error message. Otherwise, it isn’t clear where the db object is coming from or what it might do.