检查数据库中是否存在表的问题
问题描述:
基本上,我的MySQL dbname = test和表名= page.
Basically I have my MySQL dbname = test and my table name = page.
我想使用php PDO创建查询,以检查表"page"是否存在于我的数据库"test"中
I want to create a query using a php PDO to check if the table "page" exists in my db "test"
我已经尝试了这两种方法,但是它确实起作用..第一个示例始终告诉我它不存在..即使它确实存在于我的数据库中,而第二个示例告诉我它始终存在..即使它不存在....
I've tried these 2 things but it doenst work.. the first example always tells me that it doesn't exists.. even when it does exists in my db and the 2nd example tells me that it always exists... even when it doesnt exists....
$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";
}