Check if a database table exists using PHP/PDO

前端 未结 13 1781
长情又很酷
长情又很酷 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 17:02

    Here's what worked for me. It was a combination of several answers:

    $table_name = 'your_table_here'; 
    $test = "SELECT 1 FROM " . $table_name . " LIMIT 1";
    $test = $db->query($test); //$db needs to be PDO instance
    
    if($test)
    {
        return 1; //Table exists
    }
    else
    {
        return 0; //No table in database
    }
    
    0 讨论(0)
提交回复
热议问题