【问题标题】:how to check if a table exist using pdo [duplicate]如何使用 pdo 检查表是否存在 [重复]
【发布时间】:2012-08-19 06:05:13
【问题描述】:

可能重复:
MySQL check if a table exists without throwing an exception

基本上我有我的 MySQL dbname = test 和我的表名 = page。

我想使用 php PDO 创建一个查询来检查我的数据库“test”中是否存在表“page”

我试过了,但它不起作用..它总是告诉我它不存在..即使它确实存在

if (array_search('pages',$db->query('show tables')->fetch()) !== false) { echo "the db exists";

    } else { echo "the db doesnt exists";
    // Create tableS
    //$IDB->execute();
    }

【问题讨论】:

  • 你做了哪些研究来帮助你找到答案?
  • 首先链接不显示 pdo...然后我研究了我没有得到“从 INFORMATION_SCHEMA.TABLES WHERE table_schema = 'db_name'”示例中选择用户...这就是为什么我希望有人告诉我...

标签: php mysql


【解决方案1】:

PDO 中的现有表没有预定义的测试,您必须这样做:

$pdo = new PDO($dsn,$user,$pass,$options);
$results = $pdo->query('SHOW TABLE LIKE \'page\'');
if(count($results)>0){echo 'table exists';}

【讨论】:

  • 我试过这个,但它总是返回它存在 $r1 = $db->query('SHOW TABLES LIKE \'subjects\''); if (count($r1)>0) { echo "表存在"; } else { echo "表不存在"; // 创建表 }
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-25
  • 1970-01-01
  • 2017-11-07
  • 2010-12-15
  • 1970-01-01
相关资源
最近更新 更多