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 mysql_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 mysql_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
   $dbhost 
"localhost";
   
$dbuser "yourusername";
   
$dbpass "yourpassword";
   
$conn mysql_connect($dbhost$dbuser$dbpass);
   
   if(! 
$conn ) { 
      die(
"Could not connect: " mysql_error());
   }
   echo 
"Connected successfully";
   
   
mysql_select_db"demo" );
   
mysql_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
   $dbhost 
"localhost";
   
$dbuser "yourusername";
   
$dbpass "yourpassword";
   
$conn mysql_connect($dbhost$dbuser$dbpass);
   
   if(! 
$conn ) { 
      die(
"Could not connect: " mysql_error());
   }
$db_list mysql_list_dbs($conn);
while (
$row mysql_fetch_object($db_list)) {
     echo 
$row->Database .  "<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
 $dbhost 
"localhost";
 
$dbuser "yourusername";
 
$dbpass "yourpassword";
 
$conn mysql_connect($dbhost$dbuser$dbpass);
if(! 
$conn )
{
  die(
"Could not connect: " mysql_error());
}
$sql "Drop database DEMO";
$retval mysql_query$sql$conn );
if(! 
$retval )
{
  die(
"Could not delete database: " mysql_error());
}
echo 
"Database DEMO deleted successfully\n";
mysql_close($conn);
?>

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