我想检查数据库是否存在或仅使用 PDO(如果可用)。另外,如果不可能,请为我提供有关如何使用 if else 语句检查数据库是否存在的快速解决方案。任何帮助表示赞赏。谢谢你!这是我到目前为止所做的:try { $conn = new PDO("mysql:host=localhost", "root", ""); $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); //Now since I have been connected, I want to check DB existence. if(DB exists) { //check table existence } else { //Create DB }}catch(PDOException $e){ echo "Connection failed: " . $e->getMessage();}
1 回答
慕的地10843
TA贡献1785条经验 获得超8个赞
try {
$conn = new PDO("mysql:host=localhost", "root", "");
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
//Now since I have been connected, I want to check DB existence.
$databases = $conn->query('show databases')->fetchAll(PDO::FETCH_COLUMN);
if(in_array(YOUR_DATABASE_NAME,$databases))
{
//check table existence
}
else {
//Create DB
}
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}
- 1 回答
- 0 关注
- 142 浏览
添加回答
举报
0/150
提交
取消