Check if a database table exists using PHP/PDO

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

    If you have other major actions to do within the same statement, you can use the e->errorInfo

    try{                
        //Your major statements here
    }
    catch(PDOException $e){
        if($e->errorInfo[1] == 1146){
            //when table doesn't exist
        }      
    }
    

提交回复
热议问题