Check if a database table exists using PHP/PDO

前端 未结 13 1810
长情又很酷
长情又很酷 2020-12-14 16:08

I want to check if a table with a specific name exists in a database I\'ve connected to using PHP and PDO.

It has to work on all database backends, like MySQL, SQLi

13条回答
  •  甜味超标
    2020-12-14 16:44

    Before I go on, I do realise this is a MySQL-specific solution.

    While all the solutions mentioned here may work, I (personally) like to keep PDO from throwing exceptions (personal preference, that's all).

    As such, I use the following to test for table creation instead:

    SHOW TABLES LIKE 'some_table_of_mine';
    

    There's no error state generated if the table doesn't exist, you simply get a zero resultset. Works fast and consistently for me.

提交回复
热议问题