检查MySQL上是否存在表

Dan*_*nia 7 php mysql

我正在尝试检查表是否存在,如果存在则执行某些操作.我继续收到错误,告诉我该表不存在而不是完成我的支票.这是代码:

$tableExists = $db->prepare("SHOW TABLES LIKE $table_array");
$tableExists->execute();
if($tableExists->rowCount() > 0) {
   // do some code
 } else {
   echo "Unable to add because table does not exists";
}
Run Code Online (Sandbox Code Playgroud)

更新:根据以下建议,我现在执行以下操作:

$tableExists = $db->prepare("SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = ?"); 
$tableExists->execute(array($table_array)); 
if(!is_null($tableExist)) { 
    //do something
} else {
    echo "table does not exist;
}
Run Code Online (Sandbox Code Playgroud)

但是,if语句似乎无法确定表是否存在.我还能做什么?

Hyd*_* IO 11

尝试使用information_schema询问表是否存在.就像是

SELECT 
  * 
FROM
  information_schema 
WHERE TABLE_NAME = "$table_array" 
Run Code Online (Sandbox Code Playgroud)

看看所有的东西information_schema,你会惊喜于它存储的有关你的数据库的信息:)

  • 那......是......太棒了......说我把那张桌子作为phpMyAdmin侵入表的一部分...... :) (2认同)