I want to create a database. Why is not the db created with this code?
我想创建一个数据库。为什么不使用此代码创建db ?
$dbname = 'regulations_db';
$con = mysql_connect("localhost","root","pass");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
if (mysql_num_rows(mysql_query("SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = '". $dbname ."'"))) {
echo "Database $dbname already exists.";
}
else {
mysql_query("CREATE DATABASE '". $dbname ."'",$con);
echo "Database $dbname created.";
}
This is working, but I think the first one is the best practice:
这是可行的,但我认为第一个是最佳实践:
if (mysql_query("CREATE DATABASE IF NOT EXISTS regulations_db",$con))
{
echo "Database created";
}
else
{
echo "Error creating database: " . mysql_error();
}
2 个解决方案
#1
19
Just do a simple mysql_select_db()
and if the result is false then proceed with the creation.
只需执行一个简单的mysql_select_db(),如果结果为false,则继续创建。
As an example, check out the first answer here by another very smart *er.
举个例子,看看另一个非常聪明的*er的第一个答案。
<?php
// Connect to MySQL
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
// Make my_db the current database
$db_selected = mysql_select_db('my_db', $link);
if (!$db_selected) {
// If we couldn't, then it either doesn't exist, or we can't see it.
$sql = 'CREATE DATABASE my_db';
if (mysql_query($sql, $link)) {
echo "Database my_db created successfully\n";
} else {
echo 'Error creating database: ' . mysql_error() . "\n";
}
}
mysql_close($link);
?>
#2
4
Three steps to fix this:
解决这个问题的三个步骤:
- Don’t specify the database name when connecting.
- 连接时不要指定数据库名。
- Your SQL statement should be CREATE DATABASE IF NOT EXISTS php1.
- 如果不存在php1,那么SQL语句应该是CREATE DATABASE。
- Call mysqli_select_db($link, 'php1') to make that the default database for your connection.
- 调用mysqli_select_db($link, 'php1')使其成为连接的默认数据库。
#1
19
Just do a simple mysql_select_db()
and if the result is false then proceed with the creation.
只需执行一个简单的mysql_select_db(),如果结果为false,则继续创建。
As an example, check out the first answer here by another very smart *er.
举个例子,看看另一个非常聪明的*er的第一个答案。
<?php
// Connect to MySQL
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
// Make my_db the current database
$db_selected = mysql_select_db('my_db', $link);
if (!$db_selected) {
// If we couldn't, then it either doesn't exist, or we can't see it.
$sql = 'CREATE DATABASE my_db';
if (mysql_query($sql, $link)) {
echo "Database my_db created successfully\n";
} else {
echo 'Error creating database: ' . mysql_error() . "\n";
}
}
mysql_close($link);
?>
#2
4
Three steps to fix this:
解决这个问题的三个步骤:
- Don’t specify the database name when connecting.
- 连接时不要指定数据库名。
- Your SQL statement should be CREATE DATABASE IF NOT EXISTS php1.
- 如果不存在php1,那么SQL语句应该是CREATE DATABASE。
- Call mysqli_select_db($link, 'php1') to make that the default database for your connection.
- 调用mysqli_select_db($link, 'php1')使其成为连接的默认数据库。