MySQL is a free open-source relational database management system (RDBMS) that uses Structured Query Language (SQL). A database consists of one or more tables.
You can access MySQL databases directly through PHP scripts.
When creating a new database, you only need to specify the first three arguments when instantiating a mysqli object: server name, username, and password.
These scripts ensure that each operation is clear and follows good coding practices, such as proper error handling and use of object-oriented MySQLi for database connections.
PHP uses the mysqli_query function to create or delete a MySQL database. This function takes two parameters and returns TRUE on success and FALSE on failure.
<?php
$servername = "localhost";
$username = "yourusername";
$password = "yourpassword";
// Create a connection
$conn = new mysqli($servername, $username, $password);
// Check the connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// SQL query to create a database named "demo"
$sql = "CREATE DATABASE demo";
if ($conn->query($sql) === TRUE) {
echo "Database "demo" created successfully.";
} else {
echo "Error creating database: " . $conn->error;
}
// Close the connection
$conn->close();
?>
PHP provides mysqli_connect() function to open a database connection. This function takes five parameters and returns a MySQL link identifier on success or FALSE on failure.
<?php
$servername = "localhost";
$username = "yourusername";
$password = "yourpassword";
$database = "demo";
// Create a connection
$conn = new mysqli($servername, $username, $password, $database);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected to database "demo" successfully.";
// Close connection
$conn->close();
?>
This script connects to two MySQL databases "demo1" and "demo2".
<?php
$host = "localhost";
$user = "yourusername";
$pass = "yourpassword";
$database1 = "demo1";
$database2 = "demo2";
// Connect to the first database
$dbh1 = new mysqli($host, $user, $pass, $database1);
if ($dbh1->connect_errno) {
die("Unable to connect to database "demo1": " . $dbh1->connect_error);
} else {
echo "Connected to database "demo1" successfully.";
}
// Connect to the second database
$dbh2 = new mysqli($host, $user, $pass, $database2);
if ($dbh2->connect_errno) {
die("Unable to connect to database "demo2": " . $dbh2->connect_error);
} else {
echo "Connected to database "demo2" successfully.";
}
?>
This script lists all databases on the MySQL server.
<?php
$servername = "localhost";
$username = "yourusername";
$password = "yourpassword";
// Create a connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Execute the SHOW DATABASES query
$sql = "SHOW DATABASES";
$result = $conn->query($sql);
if ($result) {
// Fetch and display databases
while ($row = $result->fetch_array(MYSQLI_NUM)) {
echo $row[0] . "<br>";
}
} else {
echo "Error executing query: " . $conn->error;
}
// Close connection
$conn->close();
?>
This script deletes a MySQL database named "demo". Warning: Dropping a database will permanently delete all its data.
<?php
$servername = "localhost";
$username = "yourusername";
$password = "yourpassword";
// Create a connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// SQL query to drop the database named demo
$sql = "DROP DATABASE demo";
if ($conn->query($sql) === TRUE) {
echo "Database "demo" deleted successfully.";
} else {
echo "Error deleting database: " . $conn->error;
}
// Close connection
$conn->close();
?>