Hello, I would like to know how I can check if a table with the same name exists with php, I am doing a validation to see if it exists, if it exists, do nothing and send me a message and if it does produce the action, I put code:
include "conexion.php";
$db->query('CREATE TABLE `daem' . date('Y') . '`
(
id int(240) auto_increment not null,
nombre varchar(40) null,edad varchar(40) null,fecha_registro TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT pk_daem PRIMARY KEY(id)
)
'); // create table
$db->query('CREATE TABLE `salud' . date('Y') . '`
(
id int(240) auto_increment not null,
nombre varchar(40) null,edad varchar(40) null,fecha_registro TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT pk_salud PRIMARY KEY(id)
)
'); // create table
if ($db->connect_error) {
echo "NO SE PRODUJO ya que existe una tabla ya creada con el mismo nombre";
} else {
echo "Si se produjo";
}
Try to produce a connect_error or a connect_errno But it tells me that the action has already occurred, that is, I already created the tables but they are already created, Thank you very much in advance!
(edited)
I am using mysqli and the $db variable is the connection variable
This question is derived from others that you have made and I think that the best thing you can do is create a table where you keep a record of the years created, which will serve to show the selector and to verify that you already have them created, for example :
When you go to create the new tables, you create a form and only leave the option to create the following period:
To process the form, you first check the user permissions, assuming that only the administrator can create new periods and also check that the period to be created is the correct one:
In the queries to create the new tables just replace
date('Y')
with$nuevoPeriodo
. If you did everything correctly, there is no need to check if the table exists or not and, in any case, you can addIF NOT EXISTS
: