Check if table exist or not in PHP

I want to check if the table in database exists, if not create a table with code

CREATE TABLE Topics (
    id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    firstname VARCHAR(30) NOT NULL,
    email VARCHAR(70) NOT NULL,
    message TEXT
    )

and if not create a table. I know I have to use it but I don't know what to use before...

Upvotes: 1

Views: 177

Answers (2)

Eng_Farghly
Eng_Farghly

Reputation: 3017

You can show tables before creating new table by run this commands together

    USE database_name; -- write your database name
    SHOW TABLES;

OR create table if not exit

CREATE TABLE IF NOT EXISTS Topics (
    id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    firstname VARCHAR(30) NOT NULL,
    email VARCHAR(70) NOT NULL,
    message TEXT
    )

Upvotes: 1

michail_w
michail_w

Reputation: 4481

If you're using MySQL, use this query:

CREATE TABLE IF NOT EXISTS Topics (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
firstname VARCHAR(30) NOT NULL,
email VARCHAR(70) NOT NULL,
message TEXT
)

It will create table only if it does not exist.

Upvotes: 4

Related Questions