J
J
JackShcherbakov2018-02-01 19:38:33
PHP
JackShcherbakov, 2018-02-01 19:38:33

How to create a SQLITE database server and how to connect to it using PHP PDO?

Hello! I've been struggling with this code for 2 days now:

$db = new PDO('sqlite:/tmp/restaurant.db');  //(1)
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$q = $db->exec("CREATE TABLE dishes (
                         dish_id INT,
                         dish_name VARCHAR(255) 
                            
)");
?>

For the life of me, I can’t understand what kind of path appears in line (1). From what I read, I only understood that this is the path to the database server, but how to create it? All I learned was how to create a data table, but this is the next step, now I need to establish a connection with SQLite.
Thanks in advance.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Maksim Fedorov, 2018-02-01
@JackShcherbakov

My advice:
Install MySQL, and in all examples replace:
to the line suggested by OKyJIucT :

<?php

$db = 'dbname'; // Название БД MySQL
$user = 'user'; // Юзер
$pass= 'password'; // пароль
$db = new PDO("mysql:host=localhost;dbname=$db", $user, $pass);

?>

When you deviate from full compliance with the lesson, it will give more understanding, in addition, it is with MySQL and Postgres that you only have to work through PDO

I
ivanitch, 2020-07-13
@amurcoder

The basics of working with SQLite in PHP are here

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question