PHP MySQLi create DataBase
PHP Tutorial » PHP MySQLi create DataBase
To create a database you must have admin privilege. It's easy to create a new MySQL database. PHP uses query function to create a MySQL database. This function takes two parameters and returns TRUE on success or FALSE on failure.
In this chapter as well as in the following chapters we will show you three methods of working with PHP MySQLi create DataBase:
- MySQLi (object oriented)
- MySQLi (procedural)
- PDO
Create DataBase MySQLi (object oriented)
The following examples create a database named "Users":
<?php
$server_name = "localhost";
$user_name = "my_username";
$password = "my_password";
// Create connection
$link = new mysqli($server_name, $user_name,
$password);
// Check connection
if ($link->connect_error) {
die("Connection failed: " . $link->connect_error);
}// Create database
$link->close();
$sql = "CREATE DATABASE Users";
if ($link->query($sql) === TRUE) {
echo "Database created successfully";
} else {
echo "Error creating database: " . $link->error;
}
?>
Related subjects:
Insert multiple rows
PHP select / view data
Conect to MySQLi
Tags: how to create database in mysql using php code, mysqli create database if not exists, php create table, mysql create database php mysql tutorial create database end table in phpmyadmin
Create DataBase MySQLi (Procedural)
Attempt MySQL server connection. Assuming you are running MySQL server with default setting ("localhost", "root", "your_password")
The following examples create a database named "Members" using MySQLi (Procedural)
<?php
$server_name = "localhost";
$user_name = "my_username";
$password = "my_password";
// Create connection
$link = mysqli_connect($server_name, $user_name, $password);
// Check connection
if (!$link) {
die("Connection failed: " . mysqli_connect_error());
}// Create database
$sql = "CREATE DATABASE Members";
if (mysqli_query($link, $sql)) {
echo "Database created successfully";
} else {
echo "Error creating database: " . mysqli_error($link);
}mysqli_close($link);
?>
Create DataBase using (PDO) extension.
PDO extension. Attempt MySQL server connection. Assuming you are running MySQL server with default setting ("localhost", "root", "your_password")
The next PDO example create a DBase "my_DB_PDO":
<?php
$server_name = "localhost";
$user_name = "my_username";
$password = "my_password";
try {
$link = new PDO("mysql:host=$server_name", $user_name, $password);
// set the PDO error mode to exception
$link->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "CREATE DATABASE my_DB_PDO";
// use exec() because no results are returned
$link->exec($sql);
echo "Database created successfully<br>";
} catch(PDOException $e) {
echo $sql . "<br />" . $e->getMessage();
}$link = null;
?>
how to create database in mysql using php code, mysqli create database if not exists, php create table, mysql create database php mysql tutorial create database end table in phpmyadmin
PHP MySQLi create DataBase - php tutorial
This tool makes it easy to create, adjust, and experiment with custom colors for the web.

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

Find here examples of creative and unique website layouts.

Find here examples of creative and unique website CSS HTML menu.