PHP MySQL Create Database

Posted by tutor | Posted in PHP Tutorial | Posted on 18-12-2009-05-2008

0

The first step is to create a database and then we can create tables in that database which we can use in our application.

To create a database in MySQL the CREATE DATABASE statement is used.

Syntax

    CREATE DATABASE database_name

The above statement runs in a querry analyzer. A querry analyzer is an interface to run database commands.  But you can run this stament directly from PHP by using the mysql_query() function. This function is used to send a query or command to a MySQL connection.

The following example creates a database called “my_db”:

    For eg:

    <?php
    $conn = mysql_connect(“localhost”,”tutor”,”tutor123″);
    if (!$conn)
    {
    die(‘Could not connect: ‘ . mysql_error());
    }
    if (mysql_query(“CREATE DATABASE my_db”,$conn))
    {
    echo “Database creation:Successful”;
    }
    else
    {
    echo “Database creation:Unsuccessful: ” . mysql_error();
    }
    mysql_close($conn);
    ?>

Write a comment

Twitter Users
Enter your personal information in the form or sign in with your Twitter account by clicking the button below.