saftutorials.com
MYSQL CREATE DATABASE

MySQL Creating Database/How to create a MySQL database?

You should have admin privilege for creating and deleting a database.PHP language provides us a mysql_query() function for execute the MySQL query.By using this we can create and delete the database.This PHP function takes two parameters, 1st one is required and another is optional.This function return TRUE value on success and FALSE value on failure.


Syntax of mysql_query function

mysql_query(sql_query, connection)


sql_query  - This parameter is required. MySQL query for creating a new database.

connection - This parameter is optional. This is the connection resource or link identifier which is returned by mysql_connect() function on success.If this is not specified then the default is last opened connection of database by mysql_connect function.  

 

Below is an example of creating a new database

<?php
 $database_host = 'localhost:3036';
 $database_user = 'root';
 $database_pass = '';
 $conn = mysql_connect($database_host, $database_user, $database_pass);
 if(! $conn ) {
   die('Could not connect: ' . mysql_error());
 }else{
   echo 'Connected successfully\n';
}
 $sql = 'CREATE Database xyz_school';
 $return_value = mysql_query( $sql, $conn );
 if(! $return_value ) {
   die('Could not create database: ' . mysql_error());
 }else{
   echo "Database xyz_school created successfully\n";
}
 mysql_close($conn);
?>

If database connection is not established due to some of the wrong values then below message will print and stop the execution

Output: Could not connect: with error details


And if the database connection is established and xyz_school database created then below the line will be printed on the screen and close the connection from the database.


Output: Connected successfully

Database xyz_school created successfully


Find us on

Facebook Google Plus Twitter Pinterest YouTube

web statistic