SQL PHP HTML ASP JavaScript articles and free scripts to download
SQL PHP MySQL Functions

PHP MySQL Creating Database

By using PHP PDO we can create database in MySQL. To create database we must have required privilege in our database connection. Here is the query to create database.

CREATE DATABASE <new_db>

To use PHP PDO we will work like this.

require "config.php"; // Database connection string
$dbo->exec("Create database newdb");

WE must be interested to know success of the query and same time to display error message in case of failure. Here is the code for that.

<?Php
require "config.php"; // Database connection string

if($dbo->exec("Create database newdb2")){
echo 'New Database created';
}else{print_r($dbo->errorInfo());
}
?>

We can create database in mysql server by using mysql_create_db function. If sufficient permission is there for the user then this function will create the database in MySQL Server. Let us try with this simple example for creating a database.

<?php

// hostname or ip of server
$servername='localhost';



// username and password
$dbusername='username';
$dbpassword='password';


$link=mysql_connect ("$servername","$dbusername","$dbpassword")
or die ( " Not able to connect to server ");

if (mysql_create_db ("new_db")) {
print ("Database created successfully <br>");
} else {
print ("Error creating database: <br><br>". mysql_error ());
}

?>


The above code will create a new database new_db in our MySQL server.

PHP5 and above

The function mysql_create_db() is not supported by PHP 5. We have to use sql command to create a database in PHP 5. Here is the code to create database in PHP 5

<?php

// hostname or ip of server
$servername='localhost';

// username and password to log onto db server
$dbusername='userid';
$dbpassword='password';

$link=mysql_connect ("$servername","$dbusername","$dbpassword")
or die ( " Not able to connect to server ");

$query="CREATE DATABASE IF NOT EXISTS new_db";
if (mysql_query("$query")) {
print ("Database created successfully <br>");
} else {
print ("Error in creating database: <br><br>". mysql_error ());
}

?>

The above code will create database in PHP 5. Note that inside the query to create database we have used IF NOT EXISTS , so there will not be any error message if database is already exist and we are trying to create with same name. Without the clause IF NOT EXISTS we will get error message if we try to create a database which is already exists.
Google+

More on PHP MySQL functions
Gurjit Singh24-03-2009
I think this is a best code which resolved my problem of database creation using php 5
omar21-07-2009
thanks.. that really helped. i was using the old function and wasnt able to create:)
Abi afif13-07-2010
Thanks you, that really helped....
simbhu04-08-2010
how do i know server name and user name in single windows system.
Ajay20-04-2012
Thanks dude.
Post Comment This is for short comments only. Use the forum for more discussions.
Name
Email( not to be displayed)Privacy Policy
1+2=This is to prevent automatic submission by spammers. Please enter the result of the sum as asked




HTML . MySQL. PHP. JavaScript. ASP. Photoshop. Articles. FORUM Contact us

©2000-2014 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer