[SOLVED] How can I create a database each year and select the year I want for a select and display the records for that year?

Issue

It turns out that I need to create databases every year incrementally and automatically with their corresponding fields and tables, since , I need each database to be able to store information regarding the type of year, for example: db2021, db2022,db2023 so successively, and that the records that were made in the year 2021 are from the year 2021, records I mean the records of the database table, the typical record, and that for example with a select, you can select the year you want and the records of that year are displayed, and clearly being a new database the ids of each table start again in 1 since there are no records.

Example of what I have tried :

<?php
$db = new Mysqli('localhost', 'user', 'password', 'database', 3306); //make connection
$db->query('CREATE DATABASE `db' . date('Y') . '`'); // create database
$db->select_db('db' . date('Y')); // select newly selected database
$db->query(file_get_contents('cambio.sql')); // import the sql file into it

select name="dbName">
   <?php
     for ($year = 2022; $year < date('Y') + 1; $year++) {
        echo '<option value="db' . $year . '">' . $year . '</option>';
     }
    ?>
 </option>

Solution

You have many ways to do differently:

  • Having a timestamp column and in query filtering whith WHERE
    YEAR(date) = ‘2022’
  • Having a column "Year" that you fill with
    YEAR(NOW())
  • And many other ways…

Answered By – Alaindeseine

Answer Checked By – Timothy Miller (BugsFixing Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *