gpt4 book ai didi

php - 检查数据库中是否存在表的问题

转载 作者:可可西里 更新时间:2023-11-01 08:20:49 26 4
gpt4 key购买 nike

基本上我有我的 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com