检查数据库中是否存在表的问题 [英] Problems checking whether a table exist or not in the db

查看:36
本文介绍了检查数据库中是否存在表的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我的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";
}

这篇关于检查数据库中是否存在表的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆