AgerNic.com
WEB DEVELOPER SITE, HTML, CSS, PHP, SQL

PHP Create MySQL Database

PHP-MySQLi Tutorial » PHP Create MySQL Database

mysql_create_db - Creates a MySQL database

Warning
This feature has become obsolete in PHP 4.3.0, and has been removed in PHP 7.0.0, along with the entire original MySQL extension. The actively developed MySQLi or PDO_MySQL extension should be used instead. Also see the MySQL guide: selecting an associated API and FAQ for more information. Alternative options for this feature include:

mysqli_query ()
PDO :: query ()

 

Create a MySQL Database Using MySQLi Object-oriented - example

Open a Connection to MySQL

Before we can access data in MySQL database, we need to be able to connect to the server:

Example: (MySQLi Object-Oriented)
<?php
$server_name = "localhost";
$user_name = "username";
$password = "password";

// Create connection
$con = new mysqli($server_name, $user_name, $password);
// Check connection
if ($con->connect_error) {
die("Connection failed: " .$con->connect_error);
}

// Create database
$sql = "CREATE DATABASE my_DB_name";
if ($con->query($sql) === TRUE) {
echo "Database created successfully";
} else {
echo "Error creating database: " . $con->error;
}

$con->close();
?>
Note:
When you create a new database, you must only specify the first three arguments to the mysqli object (servername, username and password).

 

MySQLi Procedural Example (MySQLi Procedural)

Example (MySQLi Procedural)

Example: (MySQLi Procedural)
<?php
$server_name = "localhost";
$user_name = "username";
$password = "password";

// Create connection
$con = mysqli_connect($server_name, $user_name, $password);
// Check connection
if (!$con) {
die("Connection failed: " . mysqli_connect_error());
}

// Create database
$sql = "CREATE DATABASE my_DB_name";
if (mysqli_query($con, $sql)) {
echo "Database created successfully";
} else {
echo "Error creating database: " . mysqli_error($con);
}

mysqli_close($con);
?>

 

Example (PDO) - example

Example (PDO)

Example: (PDO)
<?php
$server_name = "localhost";
$user_name = "username";
$password = "password";

try {
$con = new PDO("mysql:host=$server_name", $user_name, $password);
// set the PDO error mode to exception
$con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "CREATE DATABASE my_DBPDO_name";
// use exec() because no results are returned
$con->exec($sql);
echo "Database created successfully<br>";
} catch(PDOException $e) {
echo $sql . "<br>" . $e->getMessage();
}

$con = null;
?>
Note: In the PDO example above we have also specified a database (myDB).

 



php Create Database mysql, mysqli, if not exists, php script to create mysql database backup, create php websitewith mysql database, create pp form for mysql database, create database mysql using php, php create mysql database backup, and tables
PHP Create MySQL Database - php mysqli

Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


HTML Templates
news templates
Magnews2 is a modern and creative free magazine and news website template that will help you kick off your online project in style.


CSS HTML Layout
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.


0
Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


HTML Templates
news templates
Magnews2 is a modern and creative free magazine and news website template that will help you kick off your online project in style.


CSS HTML Layout
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.