MySQL Create DB - (MySQLi Object-oriented)
[php]
<!DOCTYPE html>
<html>
<body>
<?php
$HostName = "localhost";
$UserName = "root";
$Password = "password";
// Create connection
$connection = new mysqli($HostName, $UserName, $Password);
// Check connection
if ($connection->connect_error)
{
die("Connection failed: " . $connection->connect_error);
}
// Create database
$sql = "CREATE DATABASE accounts"; //The database name can be different also
if ($connection->query($sql) === TRUE) {
echo "Database created successfully";
}
else {
echo "Error creating database: " . $connection->error;
}
$connection->close();
?>
</body>
</html>
[/php]
Note:
While creating a new database, the first three arguments are only to be specified to the mysqli object (servername, username and password).
Note:
In order to use only a particular port, add an empty string for the database-name argument. For example, new mysqli("localhost", "username", "password", "", "port")
MySQLi Procedural
[php]
<!DOCTYPE html>
<html>
<body>
<?php
$HostName = "localhost";
$UserName = "root";
$Password = "password";
// Create connection
$connection = mysqli_connect($HostName, $UserName, $Password);
// Check connection
if (!$connection)
{
die("Connection failed: " . mysqli_connect_error());
}
// Create database
$sql = "CREATE DATABASE accounts";
if (mysqli_query($connection, $sql))
{
echo "Database created successfully";
}
else
{
echo "Error creating database: " . mysqli_error($connection);
}
mysqli_close($connection);
?>
</body>
</html>
[/php]