检查 MySQL 中是否存在表(最好通过 PHP 中的 PDO)而不引发异常的最佳方法是什么。我不想解析“SHOW TABLES LIKE”等的结果。一定有某种 bool 查询?
最佳答案
使用准备好的语句查询 information_schema 数据库看起来是最可靠、最安全的解决方案。
$sql = "SELECT 1 FROM information_schema.tables
WHERE table_schema = database() AND table_name = ?";
$stmt = $pdo->prepare($sql);
$stmt->execute([$tableName]);
$exists = (bool)$stmt->fetchColumn();
关于php - MySQL检查表是否存在而不抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21038845/