How can I check if a MySQL table exists with PHP?

前端 未结 12 1643
小鲜肉
小鲜肉 2020-12-02 13:08

As simple in theory as it sounds I\'ve done a fair amount of research and am having trouble figuring this out.

How can I check if a MySQL table exists and if it does

12条回答
  •  無奈伤痛
    2020-12-02 13:42

    You can use many different queries to check if a table exists. Below is a comparison between several:

    mysql_query('select 1 from `table_name` group by 1'); or  
    mysql_query('select count(*) from `table_name`');
    
    mysql_query("DESCRIBE `table_name`");  
    70000   rows: 24ms  
    1000000 rows: 24ms  
    5000000 rows: 24ms
    
    mysql_query('select 1 from `table_name`');  
    70000   rows: 19ms  
    1000000 rows: 23ms  
    5000000 rows: 29ms
    
    mysql_query('select 1 from `table_name` group by 1'); or  
    mysql_query('select count(*) from `table_name`');  
    70000   rows: 18ms  
    1000000 rows: 18ms  
    5000000 rows: 18ms  
    

    These benchmarks are only averages:

提交回复
热议问题