如果不存在,php mysql 创建数据库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9068767/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 06:05:30  来源:igfitidea点击:

Php mysql create database if not exists

phpmysql

提问by erdomester

I want to create a database. Why is not the db created with this code?

我想创建一个数据库。为什么不使用此代码创建数据库?

$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();
    }

回答by buley

Just do a simple mysql_select_db()and if the result is false then proceed with the creation.

只需做一个简单的mysql_select_db(),如果结果为假,则继续创建。

As an example, check out the first answer hereby another very smart StackOverflower.

例如,请查看另一个非常聪明的 StackOverflower的第一个答案

<?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);
?>

回答by Anyone

Three steps to fix this:

解决这个问题的三个步骤:

  1. Don't specify the database name when connecting.
  2. Your SQL statement should be CREATE DATABASE IF NOT EXISTS php1.
  3. Call mysqli_select_db($link, 'php1') to make that the default database for your connection.
  1. 连接时不要指定数据库名称。
  2. 你的 SQL 语句应该是 CREATE DATABASE IF NOT EXISTS php1。
  3. 调用 mysqli_select_db($link, 'php1') 使其成为连接的默认数据库。

回答by 1uffyD9

If you're using MySQLi Object-oriented method, you can use following code, this code is similar to previous answer and only the method is different, I just put this because if anyone using MySQLi Object-oriented method, you can use this code directly.

如果您使用的是 MySQLi 面向对象的方法,则可以使用以下代码,此代码与之前的答案类似,只是方法不同,我之所以这么说是因为如果有人使用 MySQLi 面向对象的方法,则可以使用此代码直接地。

$servername = "localhost";
$username = "mysql_user";
$password = "user_password";
$dbName = "databaseName";

// Connect to MySQL
$conn = new mysqli($servername, $username, $password);
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// If database is not exist create one
if (!mysqli_select_db($conn,$dbName)){
    $sql = "CREATE DATABASE ".$dbName;
    if ($conn->query($sql) === TRUE) {
        echo "Database created successfully";
    }else {
        echo "Error creating database: " . $conn->error;
    }
} 

Furthermore you can refer W3school site here.

此外,您可以在此处参考 W3school 网站

Good Luck! :D

祝你好运!:D