推荐:《PHP视频教程》
创建MySQL或MySQL(不指定默认数据库)的新实例,并执行以下查询(类似于Marc B的评论):
SELECT COUNT(*) AS `exists` FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMATA.SCHEMA_NAME='my_database_name'
然后您可以检查键exists的值,查看数据库是否存在。
下面是一个示例代码:
// statement to execute $sql = 'SELECT COUNT(*) AS `exists` FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMATA.SCHEMA_NAME="my_database_name"'; // execute the statement $query = $mysqli->query($sql); if ($query === false) { throw new Exception($mysqli->error, $mysqli->errno); } // extract the value $row = $query->fetch_object(); $dbExists = (bool) $row->exists;