gpt4 book ai didi

php - 检测/创建MySQL数据库

转载 作者:行者123 更新时间:2023-11-29 09:13:01 25 4
gpt4 key购买 nike

为什么此代码无法识别现有数据库?

$result = mysql_query("SHOW DATABASES LIKE $database", $conn);
echo $result." aaaaaaaaaaaaaaaaaaa<br>";
if ($result)
{
echo ("Datebase <font color='#FF0000'><b> $database </b></font> already exist.<br /><br />");
}
else
{
mysql_query("create database IF NOT EXISTS $database CHARACTER SET utf8 COLLATE utf8_general_ci");
echo ("Datebase <font color='#FF0000'><b> $database </b></font> is succesfully created.<br /><br />");
}

最佳答案

这段代码让我有点恶心......

这是一个很好的代码:

<?php

$query = "SHOW DATABASES LIKE ". $database;
$mysqlquery = mysql_query($query);
if($mysqlquery){
echo '<b>Database already exist.<br>';
}
else
{
$createquery = "CREATE DATABASE IF NOT EXISTS ". $database ." CHARACTER SET utf8 COLLATE utf8_general_ci";
$mysqlcreatequery = mysql_query($createquery);
if($mysqlcreatequery){
echo 'Database '. $database .' created. ';
}
else
{
echo 'Database is not created!';
}
}
?>

关于php - 检测/创建MySQL数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4984345/

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