基本上我有我的 MySQL dbname = test 和我的表名 = page。
我想使用 php PDO 创建一个查询来检查表“page”是否存在于我的数据库“test”中
我已经尝试了这两种方法,但它确实有效。第一个例子总是告诉我它不存在。即使它确实存在于我的数据库中,而第二个例子告诉我它总是存在。 . 即使它不存在......
$db = new PDO('mysql:host=' . $DB_SERVER . ';dbname=' . $DB_NAME, $DB_USER, $DB_PASS);
if (array_search('pages', $db->query('show tables')->fetch()) !== false) {
echo "the db exists";
} else {
echo "the db doesnt exists";
}
这个我也试过
$results = $db->query('SHOW TABLE LIKE \'page\'');
if (count($results) > 0) {
echo 'table exists';
} else {
echo "it doesnt";
}
最佳答案
怎么样:
$results = $db->query('SHOW TABLES LIKE \'page\'');
if (count($results->fetchAll()) > 0) {
echo 'table exists';
} else {
echo "it doesnt";
}
关于php - 检查数据库中是否存在表的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12097991/