加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 检查MySQL上是否存在表

发布时间:2020-12-13 18:11:28 所属栏目:PHP教程 来源:网络整理
导读:我正在尝试检查表是否存在,如果存在,则执行一些操作.我继续收到一个错误,告诉我该表不存在而不是完成我的检查.这是代码: $tableExists = $db-prepare("SHOW TABLES LIKE $table_array");$tableExists-execute();if($tableExists-rowCount() 0) { // do some
我正在尝试检查表是否存在,如果存在,则执行一些操作.我继续收到一个错误,告诉我该表不存在而不是完成我的检查.这是代码:
$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";
}

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

$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;
}

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

尝试使用information_schema询问表是否存在.就像是
SELECT 
  * 
FROM
  information_schema 
WHERE TABLE_NAME = "$table_array"

看看information_schema所拥有的所有内容,您将对其存储的有关数据库的信息感到惊喜:)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读