Select Chapter ❯
MySQL Tutorial
MySQL Create, Select, Drop Database
You can easily create, select, drop any database using PHP functions made for MySQL
Creating a Database
You can create MySQL Database easily with PHP mysql_query() function.This mysql_query() function is used to query the database whenever we need to do operations in database we use mysql_query() function it returns TRUE on success or FALSE on failure.
You would need special privileges to create or to delete a MySQL database.
Syntax
Code Explanation
- sql: is the query used to create or delete the database
- connection: it is an optional parameter if not specified, then last opened connection by mysql_connect will be used.
Example of Creating a Database
<?php
$host = 'localhost:3036';
$user = 'root';
$pass = '';
$conn = mysql_connect($host, $user, $pass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
$sql = 'CREATE DATABASE Demodata';
$create = mysql_query( $sql );
if(! $create )
{
die('Could not create database: ' . mysql_error());
}
echo "Database Demodata created successfully";
mysql_close($conn);
?>
When You run that program it create the database name Demodata in MySQL.
Select a Database
You can select any database easily with PHP mysql_select_db() function it returns TRUE on success or FALSE on failure.
Syntax
Code Explanation
- DatabaseName: it is the name of the database you want ot connect
- connection: it is an optional parameter if not specified, then last opened connection by mysql_connect will be used.
<?php
$host = 'localhost:3036';
$user = 'root';
$pass = '';
$conn = mysql_connect($host, $user, $pass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_select_db( 'Demodata' );
mysql_close($conn);
?>
Drop a Database
You can delete the database using PHP mysql_query() function whenever you want.
$sql = 'DROP DATABASE Demodata';
$delete = mysql_query( $sql );
if(! $delete )
{
die('Could not delete database: ' . mysql_error());
}
echo "Database Demodata deleted successfully";
In this we delete the previously created database named Demodata.
In this example we use DROP because it means delete in SQL language.
❮ PrevNext ❯


