PHP – MySQL : Create Database

 

[root@mysqlDB]# mysqladmin -u root -p create eduguru
Enter password:******

This will create a MySQL database eduguru. This is a simple example to create database called eduguru.

Example:

<?php
$dbhost = 'localhost:3036';  //If mysql server is on same server
$dbuser = 'root';   //mysql user who has the permission to create database
$dbpass = 'rootpassword';  //mysql user's password
$conn = mysql_connect($dbhost, $dbuser, $dbpass);  //Connect to mysql database
if(! $conn )
{
  die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully<br />';
$sql = 'CREATE DATABASE eduguru';
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
  die('Could not create database: ' . mysql_error());
}
echo "Database eduguru created successfully\n";
mysql_close($conn);
?>

 

Example (MySQLi Object-oriented)

<?php
$servername = “localhost”;
$username = “username”;
$password = “password”;

// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die(“Connection failed: ” . $conn->connect_error);
}

// Create database
$sql = “CREATE DATABASE eduguru”;
if ($conn->query($sql) === TRUE) {
echo “Database created successfully”;
} else {
echo “Error creating database: ” . $conn->error;
}

$conn->close();
?>

Example (MySQLi Procedural)

<?php
$servername = “localhost”;
$username = “username”;
$password = “password”;

// Create connection
$conn = mysqli_connect($servername, $username, $password);
// Check connection
if (!$conn) {
die(“Connection failed: ” . mysqli_connect_error());
}

// Create database
$sql = “CREATE DATABASE eduguru”;
if (mysqli_query($conn, $sql)) {
echo “Database created successfully”;
} else {
echo “Error creating database: ” . mysqli_error($conn);
}

mysqli_close($conn);
?>

Example (PDO)

<?php
$servername = “localhost”;
$username = “username”;
$password = “password”;

try {
$conn = new PDO(“mysql:host=$servername;dbname=eduguru”, $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = “CREATE DATABASE eduguruPDO”;
// use exec() because no results are returned
$conn->exec($sql);
echo “Database created successfully<br>”;
}
catch(PDOException $e)
{
echo $sql . “<br>” . $e->getMessage();
}

$conn = null;
?>

Related Posts

  • 69
    PHP 5 and later can work with a MySQL database using: MySQLi extension (the "i" stands for improved) PDO (PHP Data Objects) Earlier versions of PHP used the MySQL extension. However, this extension was deprecated in 2012. Open a Connection to MySQL   Example: <?php $servername = "localhost"; $username =…
    Tags: create, database, php, $conn-, $username, $password, echo, $conn, password, $sql
  • 65
    Create mysql user id after logging through root user. mysql -u root -p yourpasswordforroot CREATE USER 'monty'@'localhost' IDENTIFIED BY 'some_pass'; GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,DROP ON bankaccount*. TO 'monty'@'localhost'; FLUSH PRIVILEGES;  
    Tags: mysql, create, database
  • 49
    What is database? Database is an organized collection of information about an entity having controlled redundancy and serves multiple applications. DBMS (database management system) is an application software that is developed to create and manipulate the data in database. A query language can easily access a data in a database.…
    Tags: database, mysql
  • 47
    The mysqli_connect() function opens a new connection to the MySQL server. Syntax: mysqli_connect(host,username,password,dbname,port,socket); Parameter Description host Specifies a host name or an IP address username Specifies the MySQL username password Specifies the MySQL password dbname Optional. Specifies the default database to be used port Optional. Specifies the port number to…
    Tags: mysql, password, database, php
  • 42
    Summary: in this tutorial, you will learn how to create views in MySQL by using the CREATE VIEW statement. Introduction to CREATE VIEW statement The syntax of creating a view in MySQL is as follows:   1234 5   CREATE[ALGORITHM = {MERGE  | TEMPTABLE | UNDEFINED}]VIEW [database_name].[view_name]AS [SELECT  statement] Algorithms The algorithm attribute allows you to control…
    Tags: create, mysql

Satya Prakash

VOIP Expert: More than 8 years of experience in Asterisk Development and Call Center operation Management. Unique Combination of Skill Set as IT, Analytics and operation management.

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: