r/PHPhelp • u/sltrsd • Dec 02 '25
How to run script that populates DB only once?
So I have this personal project for learning purposes that use HTML forms, PHP, PDO and SQLite. I have a separate init SQL file, which I need to be ran first and only once. This init SQL file creates SQLite database schema and populates it, where the form's <select> content is then fetched.
My current code is like this inside try block:
$pdo = new \PDO($dsn);
$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
$init_sql = file_get_contents("./init.sql");
$pdo->exec($init_sql);
$asd = $pdo->query("SELECT * FROM routes");
$routes = $asd->fetchAll();
This works but the problem is that db file is populated again with that same data from init_sql file every time page is reloaded.
I tried to put the two init_sql rows inside if clause:
if(!file_exists($db)) {
$init_sql = file_get_contents("./init.sql");
$pdo->exec($init_sql);
}
But this only creates the db file, but does not run the SQL commands. Setting init_sql and pdo as global variable did not help.
3
u/colshrapnel Dec 02 '25 edited Dec 02 '25
Why don't you just provide an installation script, instead of running this code every single time this script is running. Not a big deal but it just looks... untidy.
Also note that there shouldn't be a (dedicated to PDO) "try block", despite what your AI bot says.
Also, make sure the db path in DSN is absolute and not relative (as in the init file path). Otherwise it will create multiple db files depends on where this script is called.
3
u/jefrancomix Dec 02 '25
I'd suggest drawing inspiration from migration libraries (like Laravel), they use a control table where the already applied migrations are not run again. It's merely a relation of already applied migrations (each table or logical set of tables should be one) and the date and batch order they were applied.
You could even try to use the library itself and run the pending migrations on every initialization. You just need to use composer. But if you want to learn the most of using such a library but keep building your own infrastructure, just checking with one select if the pending migrations have been run it's enough.
2
u/eurosat7 Dec 02 '25
Another approach would be to have a different url (or shell script) to call if you want to create a db. Checking for each request is excessive.
1
u/[deleted] Dec 02 '25
Use a Migrations table.
Your sql script would do this.
The script file grows over time, but will always only apply the modifications that are missing.
Also, this script should run on deployment, not when a page is served.
1
u/bogdan_d Dec 26 '25
Do not execute init.sql each time; make a separate PHP script, let's call it configure.php, and run it once during project setup. That's a way to go, not exactly like I proposed, but in general, in projects you'll have to do a bunch of steps to set up the project (set proper env. variables, run migrations to build database schema etc.), and all this you need to make once when setting up a project on the machine, so it needs to be done separately and not by application flow