MySQL is a freely available 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 you create a new database, you must only specify the first three arguments to the mysqli object (servername, username and password).

Create (set up, install) MySql database "demo"

PHP uses mysqli_query function to create or delete a MySQL database. This function takes two parameters and returns TRUE on success or FALSE on failure.


<?php
   $servername 
="localhost";
   
$username ="yourusername";
   
$password"yourpassword";
  
// Creating a connection
$conn = new mysqli($servername$username$password);
// Check connection
if ($conn->connect_error) {
    die(
"Connection failed: " $conn->connect_error);

// Creating 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;
}

// closing connection
$conn->close();
?>
Connection (open, select) MySql database "demo"

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 "your username";
$password "your password";
$database "demo";
// Create connection

$conn mysqli_connect($servername$username$password$database);

// Check connection

if ($conn->connect_error) {
die(
"Connection failed: " $conn->connect_error);
}

echo 
"Database DEMO successfully connected";

mysqli_close($conn);

?>

Connection multiple MySQL databases on a single webpage

<?php
$host 
="localhost";
$user ="yourusername";
$pass="yourpassword";
$database1 ="demo1";
$database2 ="demo2";
$dbh1 = new mysqli($host$user$pass$database1);
if(
$dbh1->connect_errno 0){
    die(
"Unable to connect to database" $dbh1->connect_error);
}else{
    echo 
"Database demo1 is connected.";
}
 
$dbh2 = new mysqli($host$user$pass$database2);
if(
$dbh2->connect_errno 0){
    die(
"Unable to connect to database" $dbh2->connect_error);
}else{
    echo 
"Database demo2 is connected.";
}
?>

Show lists databases on MySql server

<?php
   $server 
"localhost";
   
$dbuser "yourusername";
   
$dbpass "yourpassword";

$dbh = new PDO"mysql:host=$server"$dbuser$dbpass );
$dbs $dbh->query"SHOW DATABASES" );

while( ( 
$db $dbs->fetchColumn) ) !== false )
{
    echo 
$db."<br>";
}
?>

Delete (drop) MySql database

Warning: Dropping the database is potentially a very bad thing to do. Any data stored in the database will be destroyed.


<?php
$link 
mysqli_connect("localhost""yourusername""yourpassword"); 
  
if(
$link === false){ 
    die(
"ERROR: Could not connect. " mysqli_connect_error()); 

  
$sql "DROP database DEMO"
if(
mysqli_query($link$sql)){ 
    echo 
"Database DEMO deleted successfully."
}  
else{ 
    echo 
"ERROR: Could not able to execute $sql. "  
                                   
mysqli_error($link); 

mysqli_close($link); 
?> 



PHP MySQL Tutorial: Connect to a MySQL Database (YouTube)