Search code examples
phpmysqlsqlcreate-table

How to skip table in database using php


I create a table using MySQL

$sql = "CREATE TABLE Register "

inside PHP file.

then I have INSERT data in the table using INSERT code.

insert recode successfully ,but i can insert data only one time .another time server display "massage err Table 'register' already exists . "

how can i slove this problem.i want code for

if( table is alredy in database )
{
    don't create new one 
}

else 
{    
     create database 
}

tell me how can i complete


Solution

  • No need for PHP logic here. MySQL's create table statement accepts option if not exists, so you can just do:

    create table if not exists register (
        -- your table definition goes here
    );
    

    If the table does not exists, it is created. If it already exists, the statement turns to a no-op and returns without failing.