Check if a database table exists using PHP/PDO

前端 未结 13 1811
长情又很酷
长情又很酷 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:43

    This seems to work at least with SQLite3 without exceptions, etc:

            $select =
                "SELECT CASE WHEN EXISTS (SELECT * FROM SQLITE_MASTER WHERE TYPE = 'table' AND NAME = :tableName) THEN 1 ELSE 0 END AS TABLE_EXISTS;";
    

提交回复
热议问题